create-vike 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -54
- package/readme.md +3 -0
- package/README.md +0 -30
- package/create-vike.js +0 -2
- package/dist/index.js +0 -405
- package/files/js/react/pages/about/index.page.jsx +0 -13
- package/files/js/react/pages/index/Counter.jsx +0 -12
- package/files/js/react/pages/index/index.page.jsx +0 -19
- package/files/js/react/renderer/Link.jsx +0 -15
- package/files/js/react/renderer/PageWrapper.jsx +0 -89
- package/files/js/react/renderer/_default.page.client.jsx +0 -19
- package/files/js/react/renderer/_default.page.server.jsx +0 -47
- package/files/js/react/renderer/_error.page.jsx +0 -21
- package/files/js/react/renderer/usePageContext.jsx +0 -18
- package/files/js/react+client-router/renderer/_default.page.client.jsx +0 -43
- package/files/js/react+client-router/renderer/types.js +0 -1
- package/files/js/vike/server/index.js +0 -40
- package/files/js/vue/pages/about/index.page.vue +0 -11
- package/files/js/vue/pages/index/Counter.vue +0 -10
- package/files/js/vue/pages/index/index.page.vue +0 -12
- package/files/js/vue/renderer/Link.vue +0 -17
- package/files/js/vue/renderer/PageWrapper.vue +0 -55
- package/files/js/vue/renderer/_default.page.client.js +0 -12
- package/files/js/vue/renderer/_default.page.server.js +0 -41
- package/files/js/vue/renderer/_error.page.vue +0 -14
- package/files/js/vue/renderer/app.js +0 -29
- package/files/js/vue/renderer/usePageContext.js +0 -18
- package/files/js/vue+client-router/renderer/_default.page.client.js +0 -23
- package/files/js/vue+client-router/renderer/app.js +0 -36
- package/files/js/vue+client-router/renderer/types.js +0 -1
- package/files/shared/.prettierrc +0 -1
- package/files/shared/react/pages/about/index.css +0 -4
- package/files/shared/react/renderer/PageWrapper.css +0 -19
- package/files/shared/vike/_gitignore +0 -121
- package/files/shared/vike/renderer/logo.svg +0 -36
- package/files/ts/react/pages/about/index.page.tsx +0 -13
- package/files/ts/react/pages/index/Counter.tsx +0 -12
- package/files/ts/react/pages/index/index.page.tsx +0 -19
- package/files/ts/react/renderer/Link.tsx +0 -19
- package/files/ts/react/renderer/PageWrapper.tsx +0 -96
- package/files/ts/react/renderer/_default.page.client.tsx +0 -21
- package/files/ts/react/renderer/_default.page.server.tsx +0 -49
- package/files/ts/react/renderer/_error.page.tsx +0 -21
- package/files/ts/react/renderer/types.ts +0 -11
- package/files/ts/react/renderer/usePageContext.tsx +0 -25
- package/files/ts/react+client-router/renderer/_default.page.client.tsx +0 -44
- package/files/ts/react+client-router/renderer/types.ts +0 -12
- package/files/ts/vike/server/index.ts +0 -40
- package/files/ts/vue/pages/about/index.page.vue +0 -11
- package/files/ts/vue/pages/index/Counter.vue +0 -10
- package/files/ts/vue/pages/index/index.page.vue +0 -12
- package/files/ts/vue/renderer/Link.vue +0 -17
- package/files/ts/vue/renderer/PageWrapper.vue +0 -55
- package/files/ts/vue/renderer/_default.page.client.ts +0 -14
- package/files/ts/vue/renderer/_default.page.server.ts +0 -43
- package/files/ts/vue/renderer/_error.page.vue +0 -14
- package/files/ts/vue/renderer/app.ts +0 -30
- package/files/ts/vue/renderer/types.ts +0 -10
- package/files/ts/vue/renderer/usePageContext.ts +0 -20
- package/files/ts/vue/vue.d.ts +0 -4
- package/files/ts/vue+client-router/renderer/_default.page.client.ts +0 -24
- package/files/ts/vue+client-router/renderer/app.ts +0 -37
- package/files/ts/vue+client-router/renderer/types.ts +0 -11
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { Counter } from "./Counter";
|
|
3
|
-
|
|
4
|
-
export { Page };
|
|
5
|
-
|
|
6
|
-
function Page() {
|
|
7
|
-
return (
|
|
8
|
-
<>
|
|
9
|
-
<h1>Welcome</h1>
|
|
10
|
-
This page is:
|
|
11
|
-
<ul>
|
|
12
|
-
<li>Rendered to HTML.</li>
|
|
13
|
-
<li>
|
|
14
|
-
Interactive. <Counter />
|
|
15
|
-
</li>
|
|
16
|
-
</ul>
|
|
17
|
-
</>
|
|
18
|
-
);
|
|
19
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { usePageContext } from "./usePageContext";
|
|
3
|
-
|
|
4
|
-
export { Link };
|
|
5
|
-
|
|
6
|
-
function Link(props: {
|
|
7
|
-
href?: string;
|
|
8
|
-
className?: string;
|
|
9
|
-
children: React.ReactNode;
|
|
10
|
-
}) {
|
|
11
|
-
const pageContext = usePageContext();
|
|
12
|
-
const className = [
|
|
13
|
-
props.className,
|
|
14
|
-
pageContext.urlPathname === props.href && "is-active",
|
|
15
|
-
]
|
|
16
|
-
.filter(Boolean)
|
|
17
|
-
.join(" ");
|
|
18
|
-
return <a {...props} className={className} />;
|
|
19
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import logo from "./logo.svg";
|
|
3
|
-
import { PageContextProvider } from "./usePageContext";
|
|
4
|
-
import "./PageWrapper.css";
|
|
5
|
-
import { Link } from "./Link";
|
|
6
|
-
import type { PageContext } from "./types";
|
|
7
|
-
|
|
8
|
-
export { PageWrapper };
|
|
9
|
-
|
|
10
|
-
function PageWrapper({
|
|
11
|
-
children,
|
|
12
|
-
pageContext,
|
|
13
|
-
}: {
|
|
14
|
-
children: React.ReactNode;
|
|
15
|
-
pageContext: PageContext;
|
|
16
|
-
}) {
|
|
17
|
-
return (
|
|
18
|
-
<React.StrictMode>
|
|
19
|
-
<PageContextProvider pageContext={pageContext}>
|
|
20
|
-
<Layout>
|
|
21
|
-
<Sidebar>
|
|
22
|
-
<Logo />
|
|
23
|
-
<Link className="navitem" href="/">
|
|
24
|
-
Home
|
|
25
|
-
</Link>
|
|
26
|
-
<Link className="navitem" href="/about">
|
|
27
|
-
About
|
|
28
|
-
</Link>
|
|
29
|
-
</Sidebar>
|
|
30
|
-
<Content>{children}</Content>
|
|
31
|
-
</Layout>
|
|
32
|
-
</PageContextProvider>
|
|
33
|
-
</React.StrictMode>
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function Layout({ children }: { children: React.ReactNode }) {
|
|
38
|
-
return (
|
|
39
|
-
<div
|
|
40
|
-
style={{
|
|
41
|
-
display: "flex",
|
|
42
|
-
maxWidth: 900,
|
|
43
|
-
margin: "auto",
|
|
44
|
-
}}
|
|
45
|
-
>
|
|
46
|
-
{children}
|
|
47
|
-
</div>
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function Sidebar({ children }: { children: React.ReactNode }) {
|
|
52
|
-
return (
|
|
53
|
-
<div
|
|
54
|
-
style={{
|
|
55
|
-
padding: 20,
|
|
56
|
-
flexShrink: 0,
|
|
57
|
-
display: "flex",
|
|
58
|
-
flexDirection: "column",
|
|
59
|
-
alignItems: "center",
|
|
60
|
-
lineHeight: "1.8em",
|
|
61
|
-
}}
|
|
62
|
-
>
|
|
63
|
-
{children}
|
|
64
|
-
</div>
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function Content({ children }: { children: React.ReactNode }) {
|
|
69
|
-
return (
|
|
70
|
-
<div
|
|
71
|
-
style={{
|
|
72
|
-
padding: 20,
|
|
73
|
-
paddingBottom: 50,
|
|
74
|
-
borderLeft: "2px solid #eee",
|
|
75
|
-
minHeight: "100vh",
|
|
76
|
-
}}
|
|
77
|
-
>
|
|
78
|
-
{children}
|
|
79
|
-
</div>
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function Logo() {
|
|
84
|
-
return (
|
|
85
|
-
<div
|
|
86
|
-
style={{
|
|
87
|
-
marginTop: 20,
|
|
88
|
-
marginBottom: 10,
|
|
89
|
-
}}
|
|
90
|
-
>
|
|
91
|
-
<a href="/">
|
|
92
|
-
<img src={logo} height={64} width={64} alt="logo" />
|
|
93
|
-
</a>
|
|
94
|
-
</div>
|
|
95
|
-
);
|
|
96
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import ReactDOM from "react-dom";
|
|
2
|
-
import React from "react";
|
|
3
|
-
import { getPage } from "vite-plugin-ssr/client";
|
|
4
|
-
import { PageWrapper } from "./PageWrapper";
|
|
5
|
-
import type { PageContext } from "./types";
|
|
6
|
-
import type { PageContextBuiltInClient } from "vite-plugin-ssr/types";
|
|
7
|
-
|
|
8
|
-
hydrate();
|
|
9
|
-
|
|
10
|
-
async function hydrate() {
|
|
11
|
-
// We do Server Routing, but we can also do Client Routing by using `useClientRouter()`
|
|
12
|
-
// instead of `getPage()`, see https://vite-plugin-ssr.com/useClientRouter
|
|
13
|
-
const pageContext = await getPage<PageContextBuiltInClient & PageContext>();
|
|
14
|
-
const { Page, pageProps } = pageContext;
|
|
15
|
-
ReactDOM.hydrate(
|
|
16
|
-
<PageWrapper pageContext={pageContext}>
|
|
17
|
-
<Page {...pageProps} />
|
|
18
|
-
</PageWrapper>,
|
|
19
|
-
document.getElementById("page-view")
|
|
20
|
-
);
|
|
21
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import ReactDOMServer from "react-dom/server";
|
|
2
|
-
import React from "react";
|
|
3
|
-
import { PageWrapper } from "./PageWrapper";
|
|
4
|
-
import { escapeInject, dangerouslySkipEscape } from "vite-plugin-ssr";
|
|
5
|
-
import logoUrl from "./logo.svg";
|
|
6
|
-
import type { PageContext } from "./types";
|
|
7
|
-
import type { PageContextBuiltIn } from "vite-plugin-ssr/types";
|
|
8
|
-
|
|
9
|
-
export { render };
|
|
10
|
-
|
|
11
|
-
// See https://vite-plugin-ssr.com/data-fetching
|
|
12
|
-
export const passToClient = ["pageProps", "urlPathname"];
|
|
13
|
-
|
|
14
|
-
async function render(pageContext: PageContextBuiltIn & PageContext) {
|
|
15
|
-
const { Page, pageProps } = pageContext;
|
|
16
|
-
const pageHtml = ReactDOMServer.renderToString(
|
|
17
|
-
<PageWrapper pageContext={pageContext}>
|
|
18
|
-
<Page {...pageProps} />
|
|
19
|
-
</PageWrapper>
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
// See https://vite-plugin-ssr.com/html-head
|
|
23
|
-
const { documentProps } = pageContext;
|
|
24
|
-
const title = (documentProps && documentProps.title) || "Vite SSR app";
|
|
25
|
-
const desc =
|
|
26
|
-
(documentProps && documentProps.description) ||
|
|
27
|
-
"App using Vite + vite-plugin-ssr";
|
|
28
|
-
|
|
29
|
-
const documentHtml = escapeInject`<!DOCTYPE html>
|
|
30
|
-
<html lang="en">
|
|
31
|
-
<head>
|
|
32
|
-
<meta charset="UTF-8" />
|
|
33
|
-
<link rel="icon" href="${logoUrl}" />
|
|
34
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
35
|
-
<meta name="description" content="${desc}" />
|
|
36
|
-
<title>${title}</title>
|
|
37
|
-
</head>
|
|
38
|
-
<body>
|
|
39
|
-
<div id="page-view">${dangerouslySkipEscape(pageHtml)}</div>
|
|
40
|
-
</body>
|
|
41
|
-
</html>`;
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
documentHtml,
|
|
45
|
-
pageContext: {
|
|
46
|
-
// We can add some `pageContext` here, which is useful if we want to do page redirection https://vite-plugin-ssr.com/page-redirection
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
|
|
3
|
-
export { Page };
|
|
4
|
-
|
|
5
|
-
function Page({ is404 }: { is404: boolean }) {
|
|
6
|
-
if (is404) {
|
|
7
|
-
return (
|
|
8
|
-
<>
|
|
9
|
-
<h1>404 Page Not Found</h1>
|
|
10
|
-
<p>This page could not be found.</p>
|
|
11
|
-
</>
|
|
12
|
-
);
|
|
13
|
-
} else {
|
|
14
|
-
return (
|
|
15
|
-
<>
|
|
16
|
-
<h1>500 Internal Server Error</h1>
|
|
17
|
-
<p>Something went wrong.</p>
|
|
18
|
-
</>
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export type PageProps = {};
|
|
2
|
-
// The `pageContext` that are available in both on the server-side and browser-side
|
|
3
|
-
export type PageContext = {
|
|
4
|
-
Page: (pageProps: PageProps) => React.ReactElement;
|
|
5
|
-
pageProps: PageProps;
|
|
6
|
-
urlPathname: string;
|
|
7
|
-
documentProps?: {
|
|
8
|
-
title?: string;
|
|
9
|
-
description?: string;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// `usePageContext` allows us to access `pageContext` in any React component.
|
|
2
|
-
// More infos: https://vite-plugin-ssr.com/pageContext-anywhere
|
|
3
|
-
|
|
4
|
-
import React, { useContext } from "react";
|
|
5
|
-
import type { PageContext } from "./types";
|
|
6
|
-
|
|
7
|
-
export { PageContextProvider };
|
|
8
|
-
export { usePageContext };
|
|
9
|
-
|
|
10
|
-
const Context = React.createContext<PageContext>(undefined as any);
|
|
11
|
-
|
|
12
|
-
function PageContextProvider({
|
|
13
|
-
pageContext,
|
|
14
|
-
children,
|
|
15
|
-
}: {
|
|
16
|
-
pageContext: PageContext;
|
|
17
|
-
children: React.ReactNode;
|
|
18
|
-
}) {
|
|
19
|
-
return <Context.Provider value={pageContext}>{children}</Context.Provider>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function usePageContext() {
|
|
23
|
-
const pageContext = useContext(Context);
|
|
24
|
-
return pageContext;
|
|
25
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import ReactDOM from "react-dom";
|
|
2
|
-
import React from "react";
|
|
3
|
-
import { useClientRouter } from "vite-plugin-ssr/client/router";
|
|
4
|
-
import { PageWrapper } from "./PageWrapper";
|
|
5
|
-
import { PageContext } from "./types";
|
|
6
|
-
|
|
7
|
-
useClientRouter({
|
|
8
|
-
async render(pageContext: PageContext) {
|
|
9
|
-
if (pageContext.isHydration) {
|
|
10
|
-
// When we render the first page. (Since we do SSR, the first page is already
|
|
11
|
-
// rendered to HTML and we merely have to hydrate it.)
|
|
12
|
-
const { Page, pageProps } = pageContext;
|
|
13
|
-
ReactDOM.hydrate(
|
|
14
|
-
<PageWrapper pageContext={pageContext}>
|
|
15
|
-
<Page {...pageProps} />
|
|
16
|
-
</PageWrapper>,
|
|
17
|
-
document.getElementById("page-view")
|
|
18
|
-
);
|
|
19
|
-
} else {
|
|
20
|
-
// When the user navigates to a new page.
|
|
21
|
-
const { Page, pageProps } = pageContext;
|
|
22
|
-
ReactDOM.render(
|
|
23
|
-
<PageWrapper pageContext={pageContext}>
|
|
24
|
-
<Page {...pageProps} />
|
|
25
|
-
</PageWrapper>,
|
|
26
|
-
document.getElementById("page-view")
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
ensureHydration: true,
|
|
32
|
-
|
|
33
|
-
prefetchLinks: true,
|
|
34
|
-
|
|
35
|
-
onTransitionStart() {
|
|
36
|
-
// Page transition started
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
onTransitionEnd() {
|
|
40
|
-
// Page transition ended
|
|
41
|
-
},
|
|
42
|
-
}).hydrationPromise.then(() => {
|
|
43
|
-
// Hydration finished; page is now interactive
|
|
44
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type PageProps = {};
|
|
2
|
-
// The `pageContext` that are available in both on the server-side and browser-side
|
|
3
|
-
export type PageContext = {
|
|
4
|
-
Page: (pageProps: PageProps) => React.ReactElement;
|
|
5
|
-
isHydration: boolean;
|
|
6
|
-
pageProps: PageProps;
|
|
7
|
-
urlPathname: string;
|
|
8
|
-
documentProps?: {
|
|
9
|
-
title?: string;
|
|
10
|
-
description?: string;
|
|
11
|
-
};
|
|
12
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import express from "express";
|
|
2
|
-
import { createPageRenderer } from "vite-plugin-ssr";
|
|
3
|
-
|
|
4
|
-
const isProduction = process.env.NODE_ENV === "production";
|
|
5
|
-
const root = `${__dirname}/..`;
|
|
6
|
-
|
|
7
|
-
startServer();
|
|
8
|
-
|
|
9
|
-
async function startServer() {
|
|
10
|
-
const app = express();
|
|
11
|
-
|
|
12
|
-
let viteDevServer;
|
|
13
|
-
if (isProduction) {
|
|
14
|
-
app.use(express.static(`${root}/dist/client`));
|
|
15
|
-
} else {
|
|
16
|
-
const vite = require("vite");
|
|
17
|
-
viteDevServer = await vite.createServer({
|
|
18
|
-
root,
|
|
19
|
-
server: { middlewareMode: true },
|
|
20
|
-
});
|
|
21
|
-
app.use(viteDevServer.middlewares);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const renderPage = createPageRenderer({ viteDevServer, isProduction, root });
|
|
25
|
-
app.get("*", async (req, res, next) => {
|
|
26
|
-
const url = req.originalUrl;
|
|
27
|
-
const pageContextInit = {
|
|
28
|
-
url,
|
|
29
|
-
};
|
|
30
|
-
const pageContext = await renderPage(pageContextInit);
|
|
31
|
-
const { httpResponse } = pageContext;
|
|
32
|
-
if (!httpResponse) return next();
|
|
33
|
-
const { statusCode, body } = httpResponse;
|
|
34
|
-
res.status(statusCode).send(body);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
const port = process.env.PORT || 3000;
|
|
38
|
-
app.listen(port);
|
|
39
|
-
console.log(`Server running at http://localhost:${port}`);
|
|
40
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<a :class="{ active: pageContext.urlPathname === $attrs.href }">
|
|
3
|
-
<slot />
|
|
4
|
-
</a>
|
|
5
|
-
</template>
|
|
6
|
-
<style scoped>
|
|
7
|
-
a {
|
|
8
|
-
padding: 3px 10px;
|
|
9
|
-
}
|
|
10
|
-
a.active {
|
|
11
|
-
background-color: #eee;
|
|
12
|
-
}
|
|
13
|
-
</style>
|
|
14
|
-
<script lang="ts" setup>
|
|
15
|
-
import { usePageContext } from "./usePageContext";
|
|
16
|
-
const pageContext = usePageContext();
|
|
17
|
-
</script>
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div class="layout">
|
|
3
|
-
<div class="navigation">
|
|
4
|
-
<a href="/" class="logo">
|
|
5
|
-
<img src="./logo.svg" height="64" width="64" alt="logo" />
|
|
6
|
-
</a>
|
|
7
|
-
<Link href="/">Home</Link>
|
|
8
|
-
<Link href="/about">About</Link>
|
|
9
|
-
</div>
|
|
10
|
-
<div class="content"><slot /></div>
|
|
11
|
-
</div>
|
|
12
|
-
</template>
|
|
13
|
-
|
|
14
|
-
<script lang="ts" setup>
|
|
15
|
-
import Link from "./Link.vue";
|
|
16
|
-
</script>
|
|
17
|
-
|
|
18
|
-
<style>
|
|
19
|
-
body {
|
|
20
|
-
margin: 0;
|
|
21
|
-
font-family: sans-serif;
|
|
22
|
-
}
|
|
23
|
-
* {
|
|
24
|
-
box-sizing: border-box;
|
|
25
|
-
}
|
|
26
|
-
a {
|
|
27
|
-
text-decoration: none;
|
|
28
|
-
}
|
|
29
|
-
</style>
|
|
30
|
-
|
|
31
|
-
<style scoped>
|
|
32
|
-
.layout {
|
|
33
|
-
display: flex;
|
|
34
|
-
max-width: 900px;
|
|
35
|
-
margin: auto;
|
|
36
|
-
}
|
|
37
|
-
.content {
|
|
38
|
-
padding: 20px;
|
|
39
|
-
border-left: 2px solid #eee;
|
|
40
|
-
padding-bottom: 50px;
|
|
41
|
-
min-height: 100vh;
|
|
42
|
-
}
|
|
43
|
-
.navigation {
|
|
44
|
-
padding: 20px;
|
|
45
|
-
flex-shrink: 0;
|
|
46
|
-
display: flex;
|
|
47
|
-
flex-direction: column;
|
|
48
|
-
align-items: center;
|
|
49
|
-
line-height: 1.8em;
|
|
50
|
-
}
|
|
51
|
-
.logo {
|
|
52
|
-
margin-top: 20px;
|
|
53
|
-
margin-bottom: 10px;
|
|
54
|
-
}
|
|
55
|
-
</style>
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { getPage } from "vite-plugin-ssr/client";
|
|
2
|
-
import { createApp } from "./app";
|
|
3
|
-
import type { PageContext } from "./types";
|
|
4
|
-
import type { PageContextBuiltInClient } from "vite-plugin-ssr/types";
|
|
5
|
-
|
|
6
|
-
hydrate();
|
|
7
|
-
|
|
8
|
-
async function hydrate() {
|
|
9
|
-
// We do Server Routing, but we can also do Client Routing by using `useClientRouter()`
|
|
10
|
-
// instead of `getPage()`, see https://vite-plugin-ssr.com/useClientRouter
|
|
11
|
-
const pageContext = await getPage<PageContextBuiltInClient & PageContext>();
|
|
12
|
-
const app = createApp(pageContext);
|
|
13
|
-
app.mount("#app");
|
|
14
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { renderToString } from "@vue/server-renderer";
|
|
2
|
-
import { escapeInject, dangerouslySkipEscape } from "vite-plugin-ssr";
|
|
3
|
-
import { createApp } from "./app";
|
|
4
|
-
import logoUrl from "./logo.svg";
|
|
5
|
-
import type { PageContext } from "./types";
|
|
6
|
-
import type { PageContextBuiltIn } from "vite-plugin-ssr/types";
|
|
7
|
-
|
|
8
|
-
export { render };
|
|
9
|
-
// See https://vite-plugin-ssr.com/data-fetching
|
|
10
|
-
export const passToClient = ["pageProps", "urlPathname"];
|
|
11
|
-
|
|
12
|
-
async function render(pageContext: PageContextBuiltIn & PageContext) {
|
|
13
|
-
const app = createApp(pageContext);
|
|
14
|
-
const appHtml = await renderToString(app);
|
|
15
|
-
|
|
16
|
-
// See https://vite-plugin-ssr.com/html-head
|
|
17
|
-
const { documentProps } = pageContext;
|
|
18
|
-
const title = (documentProps && documentProps.title) || "Vite SSR app";
|
|
19
|
-
const desc =
|
|
20
|
-
(documentProps && documentProps.description) ||
|
|
21
|
-
"App using Vite + vite-plugin-ssr";
|
|
22
|
-
|
|
23
|
-
const documentHtml = escapeInject`<!DOCTYPE html>
|
|
24
|
-
<html lang="en">
|
|
25
|
-
<head>
|
|
26
|
-
<meta charset="UTF-8" />
|
|
27
|
-
<link rel="icon" href="${logoUrl}" />
|
|
28
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
29
|
-
<meta name="description" content="${desc}" />
|
|
30
|
-
<title>${title}</title>
|
|
31
|
-
</head>
|
|
32
|
-
<body>
|
|
33
|
-
<div id="app">${dangerouslySkipEscape(appHtml)}</div>
|
|
34
|
-
</body>
|
|
35
|
-
</html>`;
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
documentHtml,
|
|
39
|
-
pageContext: {
|
|
40
|
-
// We can add some `pageContext` here, which is useful if we want to do page redirection https://vite-plugin-ssr.com/page-redirection
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div v-if="is404">
|
|
3
|
-
<h1>404 Page Not Found</h1>
|
|
4
|
-
<p>This page could not be found.</p>
|
|
5
|
-
</div>
|
|
6
|
-
<div v-else>
|
|
7
|
-
<h1>500 Internal Server Error</h1>
|
|
8
|
-
<p>Something went wrong.</p>
|
|
9
|
-
</div>
|
|
10
|
-
</template>
|
|
11
|
-
|
|
12
|
-
<script lang="ts" setup>
|
|
13
|
-
defineProps(["is404"]);
|
|
14
|
-
</script>
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { createSSRApp, defineComponent, h } from "vue";
|
|
2
|
-
import PageWrapper from "./PageWrapper.vue";
|
|
3
|
-
import { setPageContext } from "./usePageContext";
|
|
4
|
-
import type { PageContext } from "./types";
|
|
5
|
-
|
|
6
|
-
export { createApp };
|
|
7
|
-
|
|
8
|
-
function createApp(pageContext: PageContext) {
|
|
9
|
-
const { Page, pageProps } = pageContext;
|
|
10
|
-
const PageWithLayout = defineComponent({
|
|
11
|
-
render() {
|
|
12
|
-
return h(
|
|
13
|
-
PageWrapper,
|
|
14
|
-
{},
|
|
15
|
-
{
|
|
16
|
-
default() {
|
|
17
|
-
return h(Page, pageProps || {});
|
|
18
|
-
},
|
|
19
|
-
}
|
|
20
|
-
);
|
|
21
|
-
},
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const app = createSSRApp(PageWithLayout);
|
|
25
|
-
|
|
26
|
-
// Make `pageContext` available from any Vue component
|
|
27
|
-
setPageContext(app, pageContext);
|
|
28
|
-
|
|
29
|
-
return app;
|
|
30
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// Hook `usePageContext()` to make `pageContext` available from any Vue component.
|
|
2
|
-
// See https://vite-plugin-ssr.com/pageContext-anywhere
|
|
3
|
-
|
|
4
|
-
import { inject } from "vue";
|
|
5
|
-
import type { App } from "vue";
|
|
6
|
-
import { PageContext } from "./types";
|
|
7
|
-
|
|
8
|
-
export { usePageContext };
|
|
9
|
-
export { setPageContext };
|
|
10
|
-
|
|
11
|
-
const key = Symbol();
|
|
12
|
-
|
|
13
|
-
function usePageContext() {
|
|
14
|
-
const pageContext = inject(key);
|
|
15
|
-
return pageContext;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function setPageContext(app: App, pageContext: PageContext) {
|
|
19
|
-
app.provide(key, pageContext);
|
|
20
|
-
}
|
package/files/ts/vue/vue.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { useClientRouter } from "vite-plugin-ssr/client/router";
|
|
2
|
-
import { createApp } from "./app";
|
|
3
|
-
import { PageContext } from "./types";
|
|
4
|
-
|
|
5
|
-
useClientRouter({
|
|
6
|
-
async render(pageContext: PageContext) {
|
|
7
|
-
const app = createApp(pageContext);
|
|
8
|
-
app.mount("#app");
|
|
9
|
-
},
|
|
10
|
-
|
|
11
|
-
ensureHydration: true,
|
|
12
|
-
|
|
13
|
-
prefetchLinks: true,
|
|
14
|
-
|
|
15
|
-
onTransitionStart() {
|
|
16
|
-
// Page transition started
|
|
17
|
-
},
|
|
18
|
-
|
|
19
|
-
onTransitionEnd() {
|
|
20
|
-
// Page transition ended
|
|
21
|
-
},
|
|
22
|
-
}).hydrationPromise.then(() => {
|
|
23
|
-
// Hydration finished; page is now interactive
|
|
24
|
-
});
|