-
-
Notifications
You must be signed in to change notification settings - Fork 106
(ssr) - create initial template for ssr plugin #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or 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 @@ | ||
FOO="bar" | ||
OVERRIDE=1 | ||
EMPTY= |
This file contains hidden or 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 @@ | ||
FOO_DEV="bar" |
This file contains hidden or 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 @@ | ||
FOO_DEV_LOCAL="bar" |
This file contains hidden or 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,2 @@ | ||
FOO_LOCAL="bar" | ||
OVERRIDE=11 |
This file contains hidden or 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,10 @@ | ||
{ | ||
"scripts": { | ||
"dev": "cross-env node --experimental-modules ../../wmr/src/cli.js 2>&1" | ||
}, | ||
"type": "module", | ||
"devDependencies": { | ||
"cross-env": "7.0.3", | ||
"wmr": "latest" | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
import { useLocation } from './lib/loc.js'; | ||
|
||
export default function Header() { | ||
const { url }: { url: string } = useLocation(); | ||
return ( | ||
<header> | ||
<nav> | ||
<a href="/">Home</a> | ||
<a href="/about">About</a> | ||
<a href="/lazy-and-late">Lazy and Late</a> | ||
<a href="/compat">Compat</a> | ||
<a href="/class-fields">Class-Fields</a> | ||
<a href="/files">Files</a> | ||
<a href="/env">Env</a> | ||
<a href="/error">Error</a> | ||
</nav> | ||
<label> | ||
URL: | ||
<input readonly value={url} ref={c => c && (c.size = c.value.length)} /> | ||
</label> | ||
</header> | ||
); | ||
} |
This file contains hidden or 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,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>WMR Demo</title> | ||
<meta name="description" content="WMR Demo" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
<link rel="icon" href="data:" /> | ||
<link rel="preload" as="script" href="/index.tsx" crossorigin /> | ||
<link rel="stylesheet" href="/style.css" /> | ||
</head> | ||
|
||
<body> | ||
<script type="module" src="/index.tsx"></script> | ||
|
||
<!-- a little in-page console --> | ||
<!-- <script type="module" src="https://not-rawgit.glitch.me/gist/developit/60b810cbcb55e1da198a634218adcc90/raw/tiny-console.js"></script> --> | ||
|
||
<!-- test: external stylesheets don't break style HMR --> | ||
<!-- <link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css"> --> | ||
</body> | ||
</html> |
This file contains hidden or 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,53 @@ | ||
import { h, render } from 'preact'; | ||
import { LocationProvider, Router, ErrorBoundary, lazy, hydrate } from 'preact-iso'; | ||
import Home from './pages/home.js'; | ||
import NotFound from './pages/_404.js'; | ||
import Header from './header.tsx'; | ||
|
||
const About = lazy(() => import('./pages/about/index.js')); | ||
const LazyAndLate = lazy( | ||
() => | ||
new Promise(r => { | ||
setTimeout(() => { | ||
r(import('./pages/about/index.js')); | ||
}, 1.5e3); | ||
}) | ||
); | ||
const CompatPage = lazy(() => import('./pages/compat.js')); | ||
const ClassFields = lazy(() => import('./pages/class-fields.js')); | ||
const Files = lazy(() => import('./pages/files/index.js')); | ||
const Environment = lazy(async () => (await import('./pages/environment/index.js')).Environment); | ||
|
||
export function App() { | ||
return ( | ||
<LocationProvider> | ||
<div class="app"> | ||
<Header /> | ||
<ErrorBoundary> | ||
<Router> | ||
<Home path="/" /> | ||
<About path="/about" /> | ||
<LazyAndLate path="/lazy-and-late" title={'Lazy and Late'} /> | ||
<CompatPage path="/compat" /> | ||
<ClassFields path="/class-fields" /> | ||
<Files path="/files" /> | ||
<Environment path="/env" /> | ||
<NotFound default /> | ||
</Router> | ||
</ErrorBoundary> | ||
</div> | ||
</LocationProvider> | ||
); | ||
} | ||
|
||
if (typeof window !== 'undefined') { | ||
hydrate(<App />, document.body); | ||
} | ||
|
||
export async function prerender(data) { | ||
const { prerender } = await import('preact-iso'); | ||
return await prerender(<App {...data} />); | ||
} | ||
|
||
// @ts-ignore | ||
if (module.hot) module.hot.accept(u => render(<u.module.App />, document.body)); | ||
JoviDeCroock marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 @@ | ||
const NotFound = () => ( | ||
<section> | ||
<h1>404: Not Found</h1> | ||
<p>It's gone :(</p> | ||
</section> | ||
); | ||
|
||
export default NotFound; |
This file contains hidden or 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 styles from './style.module.css'; | ||
|
||
const About = ({ query, title }) => ( | ||
<section class={styles.about}> | ||
<h1>{title || 'About'}</h1> | ||
<p>My name is Jason.</p> | ||
<pre>{JSON.stringify(query)}</pre> | ||
</section> | ||
); | ||
|
||
export default About; |
3 changes: 3 additions & 0 deletions
3
packages/ssr-plugin/example/public/pages/about/style.module.css
This file contains hidden or 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 @@ | ||
.about { | ||
background: #dbcfe7; | ||
} |
This file contains hidden or 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 { Component } from 'preact'; | ||
|
||
export default class ClassFields extends Component { | ||
state = { | ||
value: 1 | ||
}; | ||
|
||
onClick = () => { | ||
this.setState(prev => ({ value: prev.value + 1 })); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<p>State: {this.state.value}</p> | ||
<button onClick={this.onClick}>click me</button> | ||
</div> | ||
); | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
import { useState } from 'preact/hooks'; | ||
import Calendar from 'react-calendar'; | ||
import 'react-calendar/dist/Calendar.css'; | ||
|
||
export default function CompatDemo() { | ||
const [value, onChange] = useState(new Date()); | ||
return <Calendar onChange={onChange} showWeekNumbers value={value} />; | ||
} |
This file contains hidden or 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 @@ | ||
export const foo = 42; |
27 changes: 27 additions & 0 deletions
27
packages/ssr-plugin/example/public/pages/environment/index.js
This file contains hidden or 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,27 @@ | ||
import './style.css'; | ||
import { foo } from './foo.js'; | ||
|
||
export function Environment() { | ||
return ( | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name {foo}</th> | ||
<th>Value</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{Object.keys(process.env) | ||
.sort() | ||
.map(key => { | ||
return ( | ||
<tr key={key}> | ||
<td>{key}</td> | ||
<td>{String(process.env[key])}</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/ssr-plugin/example/public/pages/environment/style.css
This file contains hidden or 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,16 @@ | ||
table { | ||
margin: 4rem; | ||
border-collapse: collapse; | ||
border-spacing: 0; | ||
} | ||
|
||
td, | ||
th { | ||
padding: 0.5rem 0.75rem; | ||
border: 0.0625rem solid #d0d0d0; | ||
text-align: left; | ||
} | ||
|
||
tbody tr:nth-child(even) { | ||
background: #eee; | ||
} |
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 hidden or 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,14 @@ | ||
import jpg from './img.jpg'; | ||
|
||
export default function Files() { | ||
return ( | ||
<div style="padding: 2rem;"> | ||
<h1>Files</h1> | ||
<p> | ||
jpg: {jpg} | ||
<br /> | ||
<img src={jpg} alt="" height="320" /> | ||
</p> | ||
</div> | ||
); | ||
} |
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 hidden or 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,25 @@ | ||
import styles from './style.module.css'; | ||
import { useState } from 'preact/hooks'; | ||
|
||
export default function Home() { | ||
const [count, setCount] = useState(0); | ||
|
||
return ( | ||
<> | ||
<section class={styles.home}> | ||
<h1>Home</h1> | ||
{/* Note: the string below is used in E2E tests */} | ||
<p>This is the home page.</p> | ||
<> | ||
<button style={{ width: 30 }} onClick={() => setCount(count - 1)}> | ||
- | ||
</button> | ||
<output style={{ padding: 10 }}>Count: {count}</output> | ||
<button style={{ width: 30 }} onClick={() => setCount(count + 1)}> | ||
+ | ||
</button> | ||
</> | ||
</section> | ||
</> | ||
); | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/ssr-plugin/example/public/pages/home/style.module.css
This file contains hidden or 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,4 @@ | ||
.home { | ||
background: #f6f6f6; | ||
color: #333; | ||
} |
This file contains hidden or 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,17 @@ | ||
import { promises as fs } from 'fs'; | ||
|
||
export async function ssr(data) { | ||
let tpl = await fs.readFile('./public/index.html', 'utf-8'); | ||
// The first script in the file is assumed to have a .prerender export: | ||
let script = (tpl.match(/<script(?:\s[^>]*?)?\s+src=(['"]?)([^>]*?)\1(?:\s[^>]*?)?>/) || [])[2]; | ||
if (!script) throw Error(`Unable to detect <script src="entry.js"> in your index.html.`); | ||
const { prerender } = await import(script); | ||
const result = await prerender(data); | ||
const body = typeof result === 'string' ? result : result.html; | ||
if (/<body(?:\s[^>]*?)?>/.test(tpl)) { | ||
tpl = tpl.replace(/(<body(?:\s[^>]*?)?)>/, '$1 ssr>' + body); | ||
} else { | ||
tpl += body; | ||
} | ||
return tpl; | ||
} |
This file contains hidden or 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,45 @@ | ||
html, | ||
body { | ||
margin: 0; | ||
min-height: 100%; | ||
font-family: system-ui, sans-serif; | ||
} | ||
|
||
header { | ||
display: flex; | ||
background: #ddd; | ||
} | ||
|
||
header > nav { | ||
flex: 1; | ||
display: flex; | ||
} | ||
|
||
header > nav a { | ||
padding: 10px; | ||
color: #673ab8; | ||
text-decoration: none; | ||
} | ||
|
||
header > nav a:hover { | ||
background-color: #f1e9ff; | ||
} | ||
|
||
header > label { | ||
display: flex; | ||
align-items: center; | ||
padding: 10px; | ||
color: #555; | ||
font-size: 80%; | ||
} | ||
|
||
header input { | ||
border: none; | ||
border-radius: 3px; | ||
padding: 2px 5px; | ||
font-size: 100%; | ||
} | ||
|
||
.app > section { | ||
padding: 20px; | ||
} |
This file contains hidden or 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,5 @@ | ||
import ssr from '../src/index.js'; | ||
|
||
export default options => { | ||
ssr({})(options); | ||
}; |
This file contains hidden or 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,30 @@ | ||
{ | ||
"name": "@wmr-plugins/ssr", | ||
"version": "0.1.1", | ||
"type": "module", | ||
"module": "./src/index.js", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"prepublishOnly": "yarn build" | ||
}, | ||
"author": "The Preact Authors (https://preactjs.com)", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/preactjs/wmr.git", | ||
"directory": "packages/ssr-plugin" | ||
}, | ||
"files": [ | ||
"src/index.js" | ||
], | ||
"devDependencies": { | ||
"wmr": "^1.3.2" | ||
}, | ||
"dependencies": {}, | ||
"peerDependencies": { | ||
"wmr": ">=1.4.0" | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
} | ||
} |
This file contains hidden or 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,17 @@ | ||
import middleware from './middleware.js'; | ||
|
||
export default function plugin(options) { | ||
function configure(config) { | ||
if (!config || config.mode === 'start') { | ||
config.middleware.push(middleware(config, options)); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
if (options.cwd) { | ||
return configure(options); | ||
} | ||
|
||
return configure; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will probably see this in PR #403 ( #403 (review) ) but just in case this goes under the radar for this PR #400, here it is again:
The
typeof window !== 'undefined'
browser check can be removed.References:
wmr/packages/preact-iso/hydrate.js
Line 7 in fe51b7e
https://github.com/preactjs/wmr/blob/main/packages/preact-iso/README.md#hydratejs