Skip to content

Commit

Permalink
chore: setup pages
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 17, 2024
1 parent 91149b7 commit 6927410
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 33 deletions.
4 changes: 4 additions & 0 deletions examples/vue-ssr-extra/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# vue-ssr-extra

## credits

- https://github.com/frandiox/vite-ssr
14 changes: 6 additions & 8 deletions examples/vue-ssr-extra/src/entry-client.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { createSSRApp } from "vue";
import { createPinia } from "pinia";
import Page from "./routes/page.vue";
import { createWebHistory, createRouter } from "vue-router";
import Root from "./root.vue";
import { routes } from "./routes";
import App from "./routes/layout.vue";

async function main() {
const pinia = createPinia();
const router = createRouter({
history: createWebHistory(),
routes: [{ path: "/", component: Page }],
routes,
});
await router.isReady();

const app = createSSRApp(Root);
app.use(pinia);
const app = createSSRApp(App);
app.use(router);

await router.isReady();
app.mount("#root");
}

Expand Down
16 changes: 9 additions & 7 deletions examples/vue-ssr-extra/src/entry-server.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { renderToWebStream } from "vue/server-renderer";
import { createSSRApp } from "vue";
import Page from "./routes/page.vue";
import { createMemoryHistory, createRouter } from "vue-router";
import Root from "./root.vue";
import { routes } from "./routes";
import App from "./routes/layout.vue";

// cf.
// https://github.com/frandiox/vite-ssr/blob/50461a4e0ebf431fdd96771e069a5e759e275b6b/src/vue/entry-server.ts

export async function handler(req: Request) {
const url = new URL(req.url);
const href = url.href.slice(url.origin.length);

// setup router
const router = createRouter({
history: createMemoryHistory(),
routes: [{ path: "/", component: Page }],
routes,
});
const url = new URL(req.url);
router.push(url.href.slice(url.origin.length));
await router.isReady();
router.push(href);

// render app
const app = createSSRApp(Root);
const app = createSSRApp(App);
app.use(router);
await router.isReady();

const ssrStream = renderToWebStream(app);
const html = (await import("virtual:index-html")).default;
Expand Down
24 changes: 24 additions & 0 deletions examples/vue-ssr-extra/src/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { RouteRecordRaw } from "vue-router";

export const routes: RouteRecordRaw[] = [
{
path: "/",
component: () => import("./routes/page.vue"),
name: "home",
},
{
path: "/client",
component: () => import("./routes/client/page.vue"),
name: "client",
},
{
path: "/server",
component: () => import("./routes/server/page.vue"),
name: "server",
},
{
path: "/:catchAll(.*)",
component: () => import("./routes/not-found.vue"),
name: "not-found",
},
];
17 changes: 17 additions & 0 deletions examples/vue-ssr-extra/src/routes/client/page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { ref, onMounted } from "vue";
const count = ref(0);
const hydrated = ref(false);
onMounted(() => {
hydrated.value = true;
});
</script>

<template>
<div>hydrated: {{ hydrated }}</div>
<div>Count: {{ count }}</div>
<button type="button" @click="count--">-1</button>
<button type="button" @click="count++">+1</button>
</template>
19 changes: 19 additions & 0 deletions examples/vue-ssr-extra/src/routes/layout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<h3>Vue example</h3>
<nav>
<ul>
<li>
<RouterLink to="/">Home</RouterLink>
</li>
<li>
<RouterLink to="/client">Counter (client)</RouterLink>
</li>
<li>
<RouterLink to="/server">Counter (server)</RouterLink>
</li>
</ul>
</nav>
<main>
<RouterView />
</main>
</template>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<template>
<RouterView />
<div>Not Found</div>
</template>
18 changes: 1 addition & 17 deletions examples/vue-ssr-extra/src/routes/page.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
<script setup lang="ts">
import { ref, onMounted } from "vue";
const count = ref(0);
const hydrated = ref(false);
onMounted(() => {
hydrated.value = true;
});
</script>

<template>
<div>
<div>hydrated: {{ hydrated }}</div>
<div>Count: {{ count }}</div>
<button type="button" @click="count--">-1</button>
<button type="button" @click="count++">+1</button>
</div>
<div>Home Page</div>
</template>
3 changes: 3 additions & 0 deletions examples/vue-ssr-extra/src/routes/server/page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>TODO: Server Counter</div>
</template>

0 comments on commit 6927410

Please sign in to comment.