-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
286 additions
and
230 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 @@ | ||
# `prerender-macro` Preact Example | ||
|
||
This is an example with React SSR without hotreloading and with a build process. | ||
|
||
To test it: | ||
|
||
- `bun install` | ||
- `bun run start` | ||
- Open http://localhost:1234 to see the result | ||
- Look at `dist/index.js` to verify how the static parts have been converted to HTML in string. | ||
|
||
The static component is translated to html in string in build-time: | ||
|
||
```tsx | ||
"<div>Static Component \uD83E\uDD76 Random number = 0.41381527597071954</div>"; | ||
``` |
This file contains 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,19 @@ | ||
{ | ||
"name": "preact-example", | ||
"private": true, | ||
"module": "build.tsx", | ||
"type": "module", | ||
"scripts": { | ||
"start": "bun run src/build.ts && bun run dist/index.js" | ||
}, | ||
"devDependencies": { | ||
"@types/bun": "latest" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "5.4.3" | ||
}, | ||
"dependencies": { | ||
"prerender-macro": "workspace:*", | ||
"preact-render-to-string": "6.4.1" | ||
} | ||
} |
This file contains 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 @@ | ||
import { join } from "node:path"; | ||
import prerenderMacro from "prerender-macro"; | ||
|
||
const { success, logs } = await Bun.build({ | ||
entrypoints: [join(import.meta.dir, "index.tsx")], | ||
outdir: join(import.meta.dir, "..", "dist"), | ||
target: "bun", | ||
plugins: [ | ||
prerenderMacro({ | ||
prerenderConfigPath: join(import.meta.dir, "prerender.tsx"), | ||
}), | ||
], | ||
}); | ||
|
||
if (success) console.log("Build complete ✅"); | ||
else console.error("Build failed ❌", logs); |
This file contains 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,7 @@ | ||
export default function DynamicComponent({ name }: { name: string }) { | ||
return ( | ||
<div> | ||
{name} Component 🔥 Random number = {Math.random()} | ||
</div> | ||
); | ||
} |
This file contains 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,7 @@ | ||
export default function StaticComponent({ name }: { name: string }) { | ||
return ( | ||
<div> | ||
{name} Component 🥶 Random number = {Math.random()} | ||
</div> | ||
); | ||
} |
This file contains 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 DynamicComponent from "./components/dynamic-component"; | ||
import StaticComponent from "./components/static-component" with { type: "prerender" }; | ||
import { render } from "preact-render-to-string"; | ||
|
||
Bun.serve({ | ||
port: 1234, | ||
fetch: async () => { | ||
return new Response( | ||
render( | ||
<html> | ||
<head> | ||
<title>Prerender Macro | Preact example</title> | ||
<meta charSet="utf-8" /> | ||
</head> | ||
<body> | ||
<StaticComponent name="Static" /> | ||
<DynamicComponent name="Dynamic" /> | ||
<a href="/">Refresh</a> | ||
</body> | ||
</html>, | ||
), | ||
{ headers: new Headers({ "Content-Type": "text/html" }) }, | ||
); | ||
}, | ||
}); | ||
|
||
console.log("Server running at http://localhost:1234"); |
This file contains 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,12 @@ | ||
import { render } from "preact-render-to-string"; | ||
import type { PrerenderConfig } from "prerender-macro"; | ||
|
||
export const prerenderConfig = { | ||
render: async (Component, props) => { | ||
return ( | ||
<div | ||
dangerouslySetInnerHTML={{ __html: render(<Component {...props} />) }} | ||
/> | ||
); | ||
}, | ||
} satisfies PrerenderConfig; |
This file contains 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 @@ | ||
{ | ||
"compilerOptions": { | ||
"jsxImportSource": "preact", | ||
"jsx": "react-jsx", | ||
"baseUrl": ".", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"module": "esnext", | ||
"target": "esnext", | ||
"moduleResolution": "bundler", | ||
"moduleDetection": "force", | ||
"skipLibCheck": true, | ||
"paths": { | ||
"react": ["node_modules/preact/compat"], | ||
"react-dom": ["./node_modules/preact/compat/"] | ||
}, | ||
"typeRoots": ["src/types", "./node_modules/@types"] | ||
}, | ||
"resolve": { | ||
"extensions": [".js", ".jsx", ".ts", ".tsx", ".json", ".svg", ".css"] | ||
}, | ||
"include": ["src"] | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.