-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from frandiox/extract-core
Extract core
- Loading branch information
Showing
54 changed files
with
2,779 additions
and
350 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
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
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
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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" href="/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/entry-client.js"></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 @@ | ||
{ | ||
"name": "example", | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "node ../../node_modules/vite-ssr/cli.js --port 1337 --force --ssr src/entry-server.js", | ||
"dev:spa": "vite --port 1337 --force", | ||
"build": "rm -rf dist && node ../../node_modules/vite-ssr/cli.js build", | ||
"serve:node": "node ../node-server/index vue" | ||
}, | ||
"dependencies": { | ||
"vite": "^2.5.0" | ||
} | ||
} |
Binary file not shown.
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 @@ | ||
User-agent: * Disallow: / |
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,20 @@ | ||
import './index.css' | ||
import viteSSR from 'vite-ssr/core/entry-client' | ||
|
||
export default viteSSR((context) => { | ||
const { initialState } = context | ||
console.log('Serialized state from server:', initialState) | ||
|
||
// Hydrate page if necessary | ||
const clockNode = document.querySelector('#clock') | ||
if (clockNode) { | ||
const { clock = {} } = initialState | ||
|
||
setInterval(() => { | ||
clockNode.innerHTML = new Date().toLocaleTimeString( | ||
clock.locale, | ||
clock.options | ||
) | ||
}, 200) | ||
} | ||
}) |
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,76 @@ | ||
import './index.css' | ||
import viteSSR from 'vite-ssr/core/entry-server' | ||
import { html } from './utils' | ||
|
||
// These are pages following a custom format: | ||
const pageModules = import.meta.globEager('./pages/**/*.js') | ||
|
||
// Simple server-only router (i.e. this is not an SPA!): | ||
const serverRouter = new Map() | ||
for (const [path, page] of Object.entries(pageModules)) { | ||
serverRouter.set(path.replace('./pages', '').replace('.js', ''), page.default) | ||
} | ||
|
||
const SUPPORTED_TIME_LOCALES = { | ||
en: 'en-US', | ||
es: 'es-ES', | ||
ja: 'ja-JP', | ||
} | ||
|
||
export default viteSSR(async (context) => { | ||
const { initialState, url, redirect } = context | ||
|
||
// Example of server redirection: | ||
if (url.pathname === '/redirect-test') { | ||
return redirect('/', 302) | ||
} | ||
|
||
// Example of routing: | ||
const page = serverRouter.get(url.pathname) | ||
if (page) { | ||
return page(context) | ||
} | ||
|
||
// Default page: | ||
|
||
// This could use url.pathname instead of querystring | ||
const lang = (url.searchParams.get('lang') || '').toLowerCase() | ||
const locale = SUPPORTED_TIME_LOCALES[lang] || SUPPORTED_TIME_LOCALES.en | ||
|
||
const clock = { | ||
locale, | ||
options: { timeStyle: 'full' }, | ||
} | ||
|
||
// This will be passed to the browser so it can use the same data | ||
initialState.clock = clock | ||
|
||
// This could change depending on context.url or anything else | ||
const body = html` | ||
<h1>This is my SSR rendered page using Vanilla JS!</h1> | ||
<p>And this is a clock that is hydrated in the browser:</p> | ||
<p> | ||
<strong id="clock"> | ||
${new Date().toLocaleTimeString(clock.locale, clock.options)} | ||
</strong> | ||
</p> | ||
<p> | ||
Try adding <a href="/?lang=es"><code>?lang=es</code></a> or | ||
<a href="/?lang=ja"><code>?lang=ja</code></a> to the URL! | ||
</p> | ||
<footer> | ||
<div>See also:</div> | ||
<div> | ||
<a href="/about">About Us</a> - | ||
<a href="/hello/world">Hello World</a> | ||
</div> | ||
</footer> | ||
` | ||
|
||
return { | ||
htmlAttrs: `lang="${locale}"`, | ||
headTags: `<title>Vanilla SSR</title>`, | ||
body, | ||
} | ||
}) |
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,8 @@ | ||
#app { | ||
font-family: Avenir, Helvetica, Arial, sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
text-align: center; | ||
color: #2c3e50; | ||
margin-top: 60px; | ||
} |
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 { html } from '../utils' | ||
|
||
export default function (context) { | ||
return { | ||
headTags: '<title>About</title>', | ||
body: html` | ||
<h1>About page</h1> | ||
<p>Something cool here</p> | ||
`, | ||
} | ||
} |
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 { html } from '../../utils' | ||
|
||
export default function (context) { | ||
return { | ||
headTags: '<title>Hello World</title>', | ||
body: html` | ||
<h1>This is a nested page</h1> | ||
<p>Hello world!</p> | ||
`, | ||
} | ||
} |
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,3 @@ | ||
// This is just a utility to highlight HTML in VSCode | ||
export const html = (s, ...args) => | ||
s.map((ss, i) => `${ss}${args[i] || ''}`).join('') |
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,21 @@ | ||
const { defineConfig } = require('vite') | ||
const viteSSR = require('vite-ssr/plugin') | ||
const api = require('../node-server/api') | ||
|
||
module.exports = defineConfig({ | ||
server: { | ||
fs: { | ||
// The API logic is in outside of the project | ||
strict: false, | ||
}, | ||
}, | ||
plugins: [ | ||
viteSSR(), | ||
{ | ||
// Mock API during development | ||
configureServer({ middlewares }) { | ||
api.forEach(({ route, handler }) => middlewares.use(route, handler)) | ||
}, | ||
}, | ||
], | ||
}) |
Oops, something went wrong.