-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(templates): add SSG template (#1)
* ssg template * todos * fixes * use appType * ignore error
- Loading branch information
1 parent
f5d4cce
commit 457a153
Showing
18 changed files
with
395 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,6 @@ dist-ssr | |
|
||
# Local Netlify folder | ||
.netlify | ||
|
||
./templates/**/package-lock.json | ||
./templates/**/yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite + Preact</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/index.jsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "ESNext", | ||
"moduleResolution": "bundler", | ||
"noEmit": true, | ||
"allowJs": true, | ||
"checkJs": true, | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "preact" | ||
}, | ||
"include": ["node_modules/vite/client.d.ts", "**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "example", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite build && vite preview" | ||
}, | ||
"dependencies": { | ||
"preact": "^10.16.0", | ||
"preact-render-to-string": "^6.2.0" | ||
}, | ||
"devDependencies": { | ||
"@preact/preset-vite": "^2.5.0", | ||
"vite": "^4.3.2", | ||
"vite-plugin-ssr": "^0.4.133" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import preactLogo from './assets/preact.svg'; | ||
import { useState } from 'preact/hooks' | ||
import viteLogo from '/vite.svg'; | ||
|
||
export function Page() { | ||
const [count, setCount] = useState(0); | ||
|
||
return ( | ||
<> | ||
<div> | ||
<a href="https://vitejs.dev" target="_blank"> | ||
<img src={viteLogo} class="logo" alt="Vite logo" /> | ||
</a> | ||
<a href="https://preactjs.com" target="_blank"> | ||
<img src={preactLogo} class="logo preact" alt="Preact logo" /> | ||
</a> | ||
</div> | ||
<h1>Vite + Preact</h1> | ||
<div class="card"> | ||
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button> | ||
<p> | ||
Edit <code>src/app.jsx</code> and save to test HMR | ||
</p> | ||
</div> | ||
<p class="read-the-docs">Click on the Vite and Preact logos to learn more</p> | ||
</> | ||
); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { PageContextProvider } from './usePageContext' | ||
|
||
export { PageShell } | ||
|
||
function PageShell({ pageContext, children }) { | ||
return ( | ||
<PageContextProvider pageContext={pageContext}> | ||
<main>{children}</main> | ||
</PageContextProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export { render } | ||
|
||
import { hydrate } from 'preact' | ||
import { PageShell } from './PageShell' | ||
|
||
// This render() hook only supports SSR, see https://vite-plugin-ssr.com/render-modes for how to modify render() to support SPA | ||
async function render(pageContext) { | ||
const { Page, pageProps } = pageContext | ||
if (!Page) throw new Error('Client-side render() hook expects pageContext.Page to be defined') | ||
const root = document.getElementById('app') | ||
if (!root) throw new Error('DOM element #app not found') | ||
|
||
hydrate( | ||
<PageShell pageContext={pageContext}> | ||
<Page {...pageProps} /> | ||
</PageShell>, | ||
root | ||
) | ||
} | ||
|
||
/* To enable Client-side Routing: | ||
export const clientRouting = true | ||
// !! WARNING !! Before doing so, read https://vite-plugin-ssr.com/clientRouting */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
export { render } | ||
// See https://vite-plugin-ssr.com/data-fetching | ||
export const passToClient = ['pageProps', 'urlPathname'] | ||
|
||
import { renderToString } from 'preact-render-to-string' | ||
import { PageShell } from './PageShell' | ||
import { escapeInject, dangerouslySkipEscape } from 'vite-plugin-ssr/server' | ||
|
||
async function render(pageContext) { | ||
const { Page, pageProps } = pageContext | ||
// This render() hook only supports SSR, see https://vite-plugin-ssr.com/render-modes for how to modify render() to support SPA | ||
if (!Page) throw new Error('My render() hook expects pageContext.Page to be defined') | ||
const pageHtml = renderToString( | ||
<PageShell pageContext={pageContext}> | ||
<Page {...pageProps} /> | ||
</PageShell> | ||
) | ||
|
||
// See https://vite-plugin-ssr.com/head | ||
const { documentProps } = pageContext.exports | ||
const title = (documentProps && documentProps.title) || 'Vite SSR app' | ||
const desc = (documentProps && documentProps.description) || 'App using Vite + vite-plugin-ssr' | ||
|
||
const documentHtml = escapeInject`<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<meta name="description" content="${desc}" /> | ||
<title>${title}</title> | ||
</head> | ||
<body> | ||
<div id="app">${dangerouslySkipEscape(pageHtml)}</div> | ||
</body> | ||
</html>` | ||
|
||
return { | ||
documentHtml, | ||
pageContext: { | ||
// We can add some `pageContext` here, which is useful if we want to do page redirection https://vite-plugin-ssr.com/page-redirection | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// `usePageContext` allows us to access `pageContext` in any Preact component. | ||
// See https://vite-plugin-ssr.com/pageContext-anywhere | ||
|
||
import { createContext } from 'preact' | ||
import { useContext } from 'preact/hooks' | ||
|
||
export { PageContextProvider } | ||
export { usePageContext } | ||
|
||
const Context = createContext(undefined) | ||
|
||
function PageContextProvider({ pageContext, children }) { | ||
return <Context.Provider value={pageContext}>{children}</Context.Provider> | ||
} | ||
|
||
function usePageContext() { | ||
const pageContext = useContext(Context) | ||
return pageContext | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { render } from 'preact'; | ||
import { useState } from 'preact/hooks'; | ||
import preactLogo from './assets/preact.svg'; | ||
import viteLogo from '/vite.svg'; | ||
import './style.css'; | ||
|
||
export function App() { | ||
const [count, setCount] = useState(0); | ||
|
||
return ( | ||
<> | ||
<div> | ||
<a href="https://vitejs.dev" target="_blank"> | ||
<img src={viteLogo} class="logo" alt="Vite logo" /> | ||
</a> | ||
<a href="https://preactjs.com" target="_blank"> | ||
<img src={preactLogo} class="logo preact" alt="Preact logo" /> | ||
</a> | ||
</div> | ||
<h1>Vite + Preact</h1> | ||
<div class="card"> | ||
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button> | ||
<p> | ||
Edit <code>src/app.jsx</code> and save to test HMR | ||
</p> | ||
</div> | ||
<p class="read-the-docs">Click on the Vite and Preact logos to learn more</p> | ||
</> | ||
); | ||
} | ||
|
||
render(<App />, document.getElementById('app')); |
Oops, something went wrong.