Skip to content

Commit

Permalink
revert: Support previewMiddlewareEnabled yet
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Jan 5, 2025
1 parent add0eae commit 2f7a44b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ preact({
| `renderTarget` | `string` | `"body"` | Query selector for where to insert prerender result in your HTML template |
| `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 |
| `previewMiddlewareEnabled` | `boolean` | `false` | Vite's preview server as of v5 will not use our prerendered HTML documents automatically. This option enables a middleware that will correct this, allowing you to test the result of prerendering locally |
| `previewMiddlewareFallback` | `string` | `/index.html` | Fallback path to be used when an HTML document cannot be found via the preview middleware, e.g., `/404` or `/not-found` will be used when the user requests `/some-path-that-does-not-exist` |

To prerender your app, you'll need to do these things:
Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export interface PreactPluginOptions {
* Additional routes that should be prerendered
*/
additionalPrerenderRoutes?: string[];
/**
* Vite's preview server won't use our prerendered HTML by default, this middleware correct this
*/
previewMiddlewareEnabled?: boolean;
/**
* Path to use as a fallback/404 route, i.e., `/404` or `/not-found`
*/
Expand Down Expand Up @@ -141,6 +145,16 @@ function preactPlugin({
reactAliasesEnabled = reactAliasesEnabled ?? true;
prerender = prerender ?? { enabled: false };

const prerenderPlugin = vitePrerenderPlugin(prerender);
if (!prerender.previewMiddlewareEnabled) {
const idx = prerenderPlugin.findIndex(
p => p.name == "serve-prerendered-html",

Check failure on line 151 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test

Parameter 'p' implicitly has an 'any' type.
);
if (idx > -1) {
prerenderPlugin.splice(idx, 1);
}
}

const jsxPlugin: Plugin = {
name: "vite:preact-jsx",
enforce: "pre",
Expand Down Expand Up @@ -271,7 +285,7 @@ function preactPlugin({
...(prefreshEnabled
? [prefresh({ include, exclude, parserPlugins: baseParserOptions })]
: []),
...(prerender.enabled ? [vitePrerenderPlugin(prerender)] : []),
...(prerender.enabled ? prerenderPlugin : []),
];
}

Expand Down

0 comments on commit 2f7a44b

Please sign in to comment.