Skip to content

Commit

Permalink
prerender sets
Browse files Browse the repository at this point in the history
  • Loading branch information
rossrobino committed Oct 9, 2024
1 parent e95c30c commit 8c59699
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-stingrays-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"domco": patch
---

`prerender` export can now be a `Set` in addition to being an `Array`
4 changes: 2 additions & 2 deletions apps/docs/src/server/+func.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Hono } from "hono";
import { etag } from "hono/etag";
import { raw } from "hono/html";

export const prerender: Prerender = ["/", "/api-reference"];
export const prerender: Prerender = new Set(["/", "/api-reference"]);

const app = new Hono();

Expand Down Expand Up @@ -66,7 +66,7 @@ for (const [fileName, md] of Object.entries(content)) {

const pathName = `/${slug}`;

prerender.push(pathName);
prerender.add(pathName);

app.get(pathName, (c) => {
return c.render(
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/server/content/generated/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ Helper type for a type that could be a promise.

### Prerender

> **Prerender**: `string`[]
> **Prerender**: `string`[] \| `Set`\<`string`\>
Paths to prerender at build time.

Expand Down
12 changes: 10 additions & 2 deletions apps/docs/src/server/content/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,18 @@ Export a `prerender` variable to prerender routes.

```ts
// src/server/+func.ts
export const prerender = ["/", "/post-1", "/post-2", "/some.css", "/some.json"];
import type { Prerender } from "domco";

export const prerender: Prerender = [
"/",
"/post-1",
"/post-2",
"/some.css",
"/some.json",
];
```

After the Vite build is complete, domco will import your function and request the routes provided. The responses will be written to `dist/client/(prerender-path)` files upon build. If the path does not have an extension, `/index.html` will be added to the end of the file path to write.
After the Vite build is complete, domco will import the `handler` from your function module and request the routes provided. The responses will be written to `dist/client/(prerender-path)` files upon build. If the path does not have an extension, `/index.html` will be added to the end of the file path to write.

For the export above, domco would request each path and generate the following files from the responses.

Expand Down
7 changes: 6 additions & 1 deletion packages/domco/src/plugin/lifecycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,16 @@ const prerender = async () => {

if (!prerender) return;

const staticFilePromises: Promise<StaticFile>[] = [];
if (prerender instanceof Set) {
// convert to array (can't sort a set)
prerender = Array.from(prerender);
}

// sort for logs
prerender.sort();

const staticFilePromises: Promise<StaticFile>[] = [];

for (const staticPath of prerender) {
if (!staticPath.startsWith("/")) {
throw Error(
Expand Down
2 changes: 1 addition & 1 deletion packages/domco/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type Handler = (req: Request) => MaybePromise<Response>;
* export const prerender: Prerender = ["/", "/post-1", "/post-2"];
* ```
*/
export type Prerender = Array<string>;
export type Prerender = Array<string> | Set<string>;

/** Middleware used in the Vite server for dev and preview. */
export type AdapterMiddleware = Connect.NextHandleFunction;
Expand Down

0 comments on commit 8c59699

Please sign in to comment.