Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 334ea64

Browse files
committed
wip(framework/vue): add createSSRApp and Link
1 parent e8007a5 commit 334ea64

File tree

11 files changed

+105
-94
lines changed

11 files changed

+105
-94
lines changed

examples/vue-app/app.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<script setup>
22
import { ref } from "vue"
3+
import { Link } from "aleph/vue"
34
45
const msg = ref("Hello world!")
5-
6-
function click() {
7-
location.pathname = '/hello'
8-
}
96
</script>
107

118
<template>
129
<h1 v-if="msg">{{ msg }}</h1>
1310
<h1 v-if="!msg" style="color: #ccc;">Please type something</h1>
1411
<input v-model="msg" placeholder="Please type something">
15-
<button @click="click">hello page</button>
12+
<Link to="/hello">hello page</Link>
13+
<br />
14+
<Link to="/blog">blog page</Link>
1615
</template>
1716

1817
<style scoped>

examples/vue-app/hello.vue

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/vue-app/main.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
1-
import { createApp } from "vue";
2-
import { createRouter, createWebHistory } from "vue-router";
1+
import { createSSRApp } from "aleph/vue";
32
import App from "./app.vue";
4-
import Hello from "./hello.vue";
5-
import { routes } from "./routes.js";
63

7-
const router = createRouter({
8-
history: createWebHistory(),
9-
routes,
10-
});
11-
12-
const pathname = location.pathname;
13-
const app = createApp(
14-
{
15-
"/": App,
16-
"/hello": Hello,
17-
}[pathname],
18-
);
19-
20-
app.use(router);
21-
await router.push({ path: pathname });
22-
23-
router.isReady().then(() => {
24-
app.mount("#root", true);
25-
});
4+
createSSRApp(App).mount("#root", true);

examples/vue-app/routes.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/vue-app/routes/blog.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script setup>
2+
import { ref } from "vue"
3+
import { Link } from "aleph/vue"
4+
5+
const msg = ref("blog page")
6+
</script>
7+
8+
<template>
9+
<h2>{{ msg }}</h2>
10+
<br />
11+
<Link to="/">go back</Link>
12+
</template>

examples/vue-app/routes/hello.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script setup>
2+
import { ref } from "vue"
3+
import { Link } from "aleph/vue"
4+
5+
const msg = ref("hello page")
6+
</script>
7+
8+
<template>
9+
<h2>{{ msg }}</h2>
10+
<br />
11+
<Link to="/">go back</Link>
12+
</template>

examples/vue-app/server.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
1-
import { createSSRApp } from "vue";
2-
import { createMemoryHistory, createRouter } from "vue-router";
1+
import { createSSRApp } from "aleph/vue";
32
import { serve } from "aleph/server";
43
import { renderToString } from "vue/server-renderer";
54
import App from "./app.vue";
6-
import Hello from "./hello.vue";
7-
import { routes } from "./routes.js";
8-
9-
const createSSRApp_ = async (ctx) => {
10-
const pathname = ctx.url.pathname;
11-
const ssrApp = createSSRApp(
12-
{
13-
"/": App,
14-
"/hello": Hello,
15-
}[pathname],
16-
);
17-
const router = createRouter({
18-
history: createMemoryHistory(),
19-
routes,
20-
});
21-
ssrApp.use(router);
22-
23-
await router.push({ path: pathname });
24-
await router.isReady();
25-
26-
return ssrApp;
27-
};
285

296
serve({
307
config: {
@@ -34,5 +11,5 @@ serve({
3411
// presets: [ unoPreset ],
3512
},
3613
},
37-
ssr: async (ctx) => await renderToString(await createSSRApp_(ctx)),
14+
ssr: async (ctx) => await renderToString(createSSRApp(App, { ssrContext: ctx }), ctx),
3815
});

framework/vue/context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ref } from "vue";
2+
3+
export const RouterContext = ref({
4+
url: new URL("http://localhost/"),
5+
params: {},
6+
});

framework/vue/link.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useRouter } from "./router.ts";
2+
import { defineComponent, h } from "vue";
3+
4+
export const Link = defineComponent({
5+
name: "Link",
6+
props: {
7+
to: {
8+
type: String,
9+
default: "",
10+
},
11+
},
12+
setup() {
13+
const { url, params } = useRouter();
14+
return {
15+
url,
16+
params,
17+
};
18+
},
19+
render() {
20+
return h("a", { href: this.$props.to }, this.$slots.default ? this.$slots.default() : []);
21+
},
22+
});

framework/vue/mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { createSSRApp } from "./router.ts";
1+
export { App, createApp, createSSRApp } from "./router.ts";
2+
export { Link } from "./link.ts";

0 commit comments

Comments
 (0)