Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: preactjs/preset-vite
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.8.1
Choose a base ref
...
head repository: preactjs/preset-vite
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2.8.2
Choose a head ref
  • 17 commits
  • 6 files changed
  • 2 contributors

Commits on Feb 18, 2024

  1. fix: Normalize URLs w/ trailing slashes

    rschristian authored Feb 18, 2024
    Copy the full SHA
    92a4bde View commit details

Commits on Feb 19, 2024

  1. Merge pull request #104 from preactjs/fix/seen-urls

    fix: Normalize URLs w/ trailing slashes
    rschristian authored Feb 19, 2024
    Copy the full SHA
    44d6cb9 View commit details
  2. Copy the full SHA
    728a8f9 View commit details
  3. test: Revise fetch test to use response

    rschristian committed Feb 19, 2024
    Copy the full SHA
    a551c03 View commit details
  4. Merge pull request #105 from preactjs/feat/patched-fetch-response

    feat: Return proper `Response` from patched `fetch` during prerender
    rschristian authored Feb 19, 2024
    Copy the full SHA
    070c20c View commit details

Commits on Feb 20, 2024

  1. Copy the full SHA
    85e4bb4 View commit details
  2. test: Fix demo

    rschristian committed Feb 20, 2024
    Copy the full SHA
    41cfa32 View commit details
  3. Update src/prerender.ts

    rschristian authored Feb 20, 2024
    Copy the full SHA
    b268bcf View commit details
  4. Merge pull request #107 from preactjs/feat/better-prerender-errors

    Feat: Better prerender errors
    rschristian authored Feb 20, 2024
    Copy the full SHA
    4af090d View commit details

Commits on Feb 21, 2024

  1. docs: Rewrite prerendering instructions

    rschristian authored Feb 21, 2024
    Copy the full SHA
    f0c4e53 View commit details
  2. Consistency

    rschristian authored Feb 21, 2024
    Copy the full SHA
    6ab91dc View commit details
  3. Update README.md

    rschristian authored Feb 21, 2024
    Copy the full SHA
    88f3db6 View commit details

Commits on Feb 22, 2024

  1. Merge pull request #108 from preactjs/docs/prerender-instructions

    docs: Rewrite prerendering instructions
    rschristian authored Feb 22, 2024
    Copy the full SHA
    0db4c5b View commit details
  2. refactor: Clean sourcemaps in newer Vite versions

    rschristian committed Feb 22, 2024
    Copy the full SHA
    d1a2486 View commit details
  3. refactor: Skip second loop, delete from bundle earlier

    rschristian committed Feb 22, 2024
    Copy the full SHA
    775e26d View commit details
  4. Merge pull request #109 from preactjs/refactor/clean-some-sourcemaps

    refactor: Clean sourcemaps in newer Vite versions
    rschristian authored Feb 22, 2024
    Copy the full SHA
    a25a252 View commit details

Commits on Mar 15, 2024

  1. 2.8.2 (#111)

    rschristian authored Mar 15, 2024
    Copy the full SHA
    238a9f8 View commit details
Showing with 168 additions and 36 deletions.
  1. +18 −6 README.md
  2. +3 −0 demo/package.json
  3. +2 −1 demo/src/components/LocalFetch.tsx
  4. +68 −12 package-lock.json
  5. +6 −2 package.json
  6. +71 −15 src/prerender.ts
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -75,19 +75,31 @@ preact({
| Option | Type | Default | Description |
|---|---|---|---|
| `enabled` | `boolean` | `false` | Enables prerendering |
| `prerenderScript` | `string` | `undefined` | Absolute path to script containing exported `prerender()` function. If not provided, will try to find the prerender script in the scripts listed in your HTML entrypoint |
| `renderTarget` | `string` | `"body"` | Query selector for where to insert prerender result in your HTML template |
| `additionalPrerenderRoutes` | `string` | `undefined` | Prerendering will automatically discover links to prerender, but if there are unliked pages that you want to prererender (such as a `/404` page), use this option to specify them |
| `prerenderScript` | `string` | `undefined` | Absolute path to script containing exported `prerender()` function. If not provided, will try to find the prerender script in the scripts listed in your HTML entrypoint |
| `additionalPrerenderRoutes` | `string[]` | `undefined` | Prerendering will crawl your site automatically, but you'd like to prerender some pages that may not be found (such as a `/404` page), use this option to specify them |

To prerender your app, you'll need to do these things:
1. Enable prerendering in the plugin options
2. Specify your render target, if you want the HTML to be inserted anywhere other than the `document.body`. This location likely should match `render()`, i.e., `render(<App />, document.querySelector('#app'))` -> `'#app'`
4. Create and export a `prerender()` function from a script. You could add this to your app entrypoint or create a completely separate file for it, either will work. See below for a usage example
5. Specify where your `prerender()` function is by either a) adding a `prerender` attribute to the script tag that contains it in your entry HTML (`<script prerender src="./my-prerender-script.js">`) or b) use the `prerenderScript` plugin option to specify the location with an absolute path

To prerender your app, you'll need to set `prerender.enabled` to `true` in the plugin options (`vite.config.js`), export a `prerender()` function one of the scripts listed in your HTML entry point (or the script specified through `prerender.prerenderScript`), and add a `prerender` attribute to that script tag in your HTML entry point (`<script prerender src="...">`). How precisely you generate an HTML string from your app is up to you, but you'll likely want to use [`preact-render-to-string`](https://github.com/preactjs/preact-render-to-string) or a wrapper around it such as [`preact-iso`'s `prerender`](https://github.com/preactjs/preact-iso). Whatever you choose, you simply need to return an object from your `prerender()` function containing an `html` property with your HTML string.
The plugin simply calls the prerender function you provide so it's up to you to determine how your app should be prerendered. You'll likely want to use [`preact-render-to-string`](https://github.com/preactjs/preact-render-to-string), or a wrapper around it such as [`preact-iso`'s `prerender`](https://github.com/preactjs/preact-iso), but whatever you choose, the minimum you'll need to return is an object containing an `html` property with your HTML string which will then be inserted according to your `renderTarget`.

[For an example implementation, see our demo](./demo/src/index.tsx)
Your `prerender()` function can be asynchronous, so feel free to make HTTP requests to retrieve data (`fetch(...)`), read files from disk (`fs.readFile(...)`), or similar things to set up your app.

[For a full example implementation, see our demo](./demo/src/index.tsx)

```js
import { render } from 'preact-render-to-string';
import { prerender as ssr } from 'preact-iso';

function App() {
return <h1>Hello World!</h1>
}

export async function prerender(data) {
const html = render(`<h1>hello world</h1>`);
const html = ssr(<App />);

return {
html,
3 changes: 3 additions & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
3 changes: 2 additions & 1 deletion demo/src/components/LocalFetch.tsx
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ const cache = new Map();

async function load(url: string) {
const res = await fetch(url);
return await res.text();
if (res.ok) return await res.text();
throw new Error(`Failed to fetch ${url}!`);
}

function useFetch(url: string) {
80 changes: 68 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@preact/preset-vite",
"version": "2.8.1",
"version": "2.8.2",
"description": "Preact preset for the vite bundler",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.mjs",
@@ -42,19 +42,23 @@
"kolorist": "^1.8.0",
"magic-string": "0.30.5",
"node-html-parser": "^6.1.10",
"resolve": "^1.22.8"
"resolve": "^1.22.8",
"source-map": "^0.7.4",
"stack-trace": "^1.0.0-pre2"
},
"peerDependencies": {
"@babel/core": "7.x",
"vite": "2.x || 3.x || 4.x || 5.x"
},
"devDependencies": {
"@babel/core": "^7.15.8",
"@types/babel__code-frame": "^7.0.6",
"@types/babel__core": "^7.1.14",
"@types/debug": "^4.1.5",
"@types/estree": "^0.0.50",
"@types/node": "^14.14.33",
"@types/resolve": "^1.20.1",
"@types/stack-trace": "^0.0.33",
"lint-staged": "^10.5.4",
"preact": "^10.19.2",
"preact-iso": "^2.3.2",
Loading