Skip to content
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

feat: prerender plugin #86

Merged
merged 40 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f8ed4b7
feat: Add (rough) prerender plugin
rschristian Oct 13, 2023
e8cfe8d
refactor: Switch to TS
rschristian Oct 16, 2023
19ebda4
feat: Expose prerender plugin
rschristian Oct 16, 2023
42a14c1
chore: Update demo
rschristian Oct 16, 2023
967b2d1
fix: Better preload patching
rschristian Oct 16, 2023
be0c741
fix: Bug in assumed order of files in output bundle
rschristian Oct 16, 2023
192fa26
test: Update build test case to check prerendering
rschristian Oct 16, 2023
27cf56b
chore: Formatting
rschristian Oct 16, 2023
1e43708
refactor: Appease TS
rschristian Nov 19, 2023
432cd6f
chore: Correct error reporting
rschristian Nov 19, 2023
f5c2cc9
refactor: Remove sorting of props when serializing
rschristian Nov 19, 2023
90c4b90
test: Add tests for `head` API
rschristian Nov 19, 2023
dc7bb6d
refactor: Move prerender deps into deps
rschristian Nov 19, 2023
0672fdd
refactor: Move `magic-string` back to peerDeps
rschristian Nov 19, 2023
6307723
fix: Preload patch for Vite 5
rschristian Nov 19, 2023
a0495b2
fix: `additionalPrerenderRoutes` option
rschristian Nov 19, 2023
7acf362
test: Add test for `additionalPrerenderRoute`
rschristian Nov 19, 2023
ec5c0ee
refactor: Use resolved Vite config rather than user config
rschristian Nov 21, 2023
8fe21a7
test: Fix tests
rschristian Nov 21, 2023
657c67e
feat: Add local, fs-based `fetch` during prerender
rschristian Nov 21, 2023
ba07c83
test: Add test for fs fetch during prerender
rschristian Nov 21, 2023
dce372d
refactor: Switch to `rs-module-lexer` for prerender detection
rschristian Nov 21, 2023
df96c14
chore: Consistent peer dep range
rschristian Dec 13, 2023
935d8c3
fix: `rs-module-lexer` import
rschristian Dec 13, 2023
00d4b0d
fix: Better error when prerender entry cannot be found
rschristian Dec 13, 2023
ac891fa
fix: preload patch on Vite 5.0.5+
rschristian Dec 13, 2023
e930e5a
revert: Accidental change to demo's mount point
rschristian Dec 15, 2023
a82389f
refactor: Move tmp dir local to make use of node modules
rschristian Dec 15, 2023
fee4d5e
feat: Allow for custom prerender injection selector
rschristian Dec 16, 2023
be9a099
refactor: Move `magic-string` to deps to fix yarn v1
rschristian Dec 16, 2023
23bbc75
refactor: Rename `selector` -> `renderTarget` in prerender API
rschristian Dec 17, 2023
e489a37
fix: Skip copying non-chunk JS outputs in the bundle
rschristian Dec 17, 2023
ec34721
Update src/prerender.ts
rschristian Dec 29, 2023
976638d
docs: Add docs for prerendering
rschristian Dec 29, 2023
7059c03
chore: Misc
rschristian Dec 29, 2023
909bd24
refactor: Add better error message for prerendering failure
rschristian Dec 29, 2023
2d4a6ed
chore: Better word wrapping
rschristian Dec 29, 2023
ee3ee5b
revert: Lock file changes
rschristian Dec 29, 2023
84de652
refactor: Move `renderTarget` option & document it
rschristian Dec 31, 2023
b99d77f
chore: Use Rollup's error reporting where possible
rschristian Dec 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
Expand Down
1 change: 1 addition & 0 deletions demo/public/local-fetch-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Local fetch works
32 changes: 32 additions & 0 deletions demo/src/components/LocalFetch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useState } from "preact/hooks";

const cache = new Map();

async function load(url: string) {
const res = await fetch(url);
return await res.text();
}

function useFetch(url: string) {
const [_, update] = useState({});

let data = cache.get(url);
if (!data) {
data = load(url);
cache.set(url, data);
data.then(
(res: string) => update((data.res = res)),
(err: Error) => update((data.err = err)),
);
}

if (data.res) return data.res;
if (data.err) throw data.err;
throw data;
}

export function LocalFetch() {
const data = useFetch("/local-fetch-test.txt");

return <p>{data.trimEnd()}</p>;
}
35 changes: 32 additions & 3 deletions demo/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { render } from "preact";
import { LocationProvider, Router, Route } from "preact-iso";
import {
LocationProvider,
Router,
Route,
hydrate,
prerender as ssr,
} from "preact-iso";

import { Header } from "./components/Header.jsx";
import { Home } from "./pages/Home/index.jsx";
Expand All @@ -20,4 +25,28 @@ export function App() {
);
}

render(<App />, document.getElementById("app")!);
if (typeof window !== "undefined") {
rschristian marked this conversation as resolved.
Show resolved Hide resolved
hydrate(<App />, document.getElementById("app"));
}

export async function prerender() {
rschristian marked this conversation as resolved.
Show resolved Hide resolved
const { html, links } = await ssr(<App />);
return {
html,
links,
renderTarget: "#app",
head: {
lang: "en",
title: "Prerendered Preact App",
elements: new Set([
{
type: "meta",
props: {
name: "description",
content: "This is a prerendered Preact app",
},
},
]),
},
};
}
2 changes: 2 additions & 0 deletions demo/src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReactComponent } from "../../components/Compat";
import { LocalFetch } from "../../components/LocalFetch";

import preactLogo from "../../assets/preact.svg";
import "./style.css";
Expand All @@ -11,6 +12,7 @@ export function Home() {
</a>
<h1>Get Started building Vite-powered Preact Apps </h1>
<ReactComponent />
<LocalFetch />
<section>
<Resource
title="Learn Preact"
Expand Down
6 changes: 5 additions & 1 deletion demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ import preact from "../src/index";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [preact()],
plugins: [
preact({
prerender: { enabled: true, additionalPrerenderRoutes: ["/404"] },
}),
],
});
Loading