-
Notifications
You must be signed in to change notification settings - Fork 9
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
Sebastien Ringrose
committed
Mar 9, 2024
1 parent
787c258
commit aba1aa9
Showing
48 changed files
with
1,711 additions
and
1,729 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
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
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,162 @@ | ||
import { | ||
Router, | ||
RequestContext, | ||
logger, | ||
sse, | ||
cacher, | ||
ssr, | ||
file, | ||
} from "../../index.ts"; //"https://deno.land/x/peko/mod.ts" | ||
import { renderToString } from "preact-render-to-string"; | ||
|
||
// Preact page components and HTML template for ssrHandler render logic | ||
import Home from "./src/pages/Home.ts"; | ||
import About from "./src/pages/About.ts"; | ||
import htmlTemplate from "./document.ts"; | ||
|
||
// esbuild bundling for app TS files | ||
import * as esbuild from "esbuild"; | ||
|
||
const env: Record<string, string> = | ||
(typeof Deno !== "undefined" && Deno.env.toObject()) || | ||
(typeof process !== "undefined" && process.env) || | ||
(typeof env !== "undefined" && env) || | ||
{}; | ||
|
||
const router = new Router(); | ||
router.use(logger(console.log)); | ||
|
||
// FRONTEND PAGES | ||
router.get("/", { | ||
// use cacher to serve responses from cache in prod env | ||
middleware: env.ENVIRONMENT === "production" ? cacher() : [], | ||
handler: ssr( | ||
() => { | ||
const ssrHTML = renderToString(Home(), null); | ||
return htmlTemplate({ | ||
title: "Peko", | ||
ssrHTML, | ||
entrypoint: "/src/pages/Home.ts", | ||
}); | ||
}, | ||
{ | ||
headers: new Headers({ | ||
// instruct browser to cache page in prod env | ||
"Cache-Control": | ||
env.ENVIRONMENT === "production" | ||
? "max-age=86400, stale-while-revalidate=86400" | ||
: "no-store", | ||
}), | ||
} | ||
), | ||
}); | ||
|
||
router.get( | ||
"/about", | ||
(ctx) => { | ||
ctx.state = { | ||
request_time: `${Date.now()}`, | ||
...env, | ||
}; | ||
}, | ||
ssr((ctx) => { | ||
const ssrHTML = renderToString(About(ctx.state), null); | ||
return htmlTemplate({ | ||
title: "Peko | About", | ||
ssrHTML, | ||
entrypoint: "/src/pages/About.ts", | ||
}); | ||
}) | ||
); | ||
|
||
// STATIC FILES | ||
// dynamic URL param for filename, always cache | ||
router.get("/assets/:filename", cacher(), async (ctx) => | ||
( | ||
await file( | ||
new URL( | ||
`https://raw.githubusercontent.com/sejori/peko/main/example/preact/assets/${ctx.params.filename}` | ||
), | ||
{ | ||
headers: new Headers({ | ||
// instruct browser to cache file in prod env | ||
"Cache-Control": | ||
env.ENVIRONMENT === "production" | ||
? "max-age=86400, stale-while-revalidate=86400" | ||
: "no-store", | ||
}), | ||
} | ||
) | ||
)(ctx) | ||
); | ||
|
||
// BUNDLED TS FILES | ||
// dynamic URL param for filename, always cache | ||
router.get("/src/:filename", cacher(), async (ctx) => | ||
( | ||
await file( | ||
new URL( | ||
`https://raw.githubusercontent.com/sejori/peko/main/example/preact/src/${ctx.params.filename}` | ||
), | ||
{ | ||
transform: async (contents) => { | ||
const reader = contents.getReader(); | ||
let result = ""; | ||
let { done, value } = await reader.read(); | ||
while (!done) { | ||
result += new TextDecoder().decode(value); | ||
({ done, value } = await reader.read()); | ||
} | ||
const esbuildResult = await esbuild.build({ | ||
bundle: true, | ||
write: false, | ||
stdin: { | ||
contents: result, | ||
}, | ||
format: "esm", | ||
target: "es2022", | ||
loader: { | ||
".ts": "ts", | ||
}, | ||
}); | ||
|
||
const bundle = esbuildResult.outputFiles[0].text; | ||
console.log(bundle); | ||
return bundle; | ||
}, | ||
headers: new Headers({ | ||
"Content-Type": "application/javascript", | ||
// instruct browser to cache file in prod env | ||
"Cache-Control": | ||
env.ENVIRONMENT === "production" | ||
? "max-age=86400, stale-while-revalidate=86400" | ||
: "no-store", | ||
}), | ||
} | ||
) | ||
)(ctx) | ||
); | ||
|
||
// API FUNCTIONALITY | ||
const demoEventTarget = new EventTarget(); | ||
// shorthand route adding | ||
router.get("/sse", (ctx: RequestContext) => { | ||
setInterval(() => { | ||
demoEventTarget.dispatchEvent( | ||
new CustomEvent("send", { detail: Math.random() }) | ||
); | ||
}, 2500); | ||
|
||
return sse(demoEventTarget)(ctx); | ||
}); | ||
// full route object adding | ||
router.addRoute({ | ||
path: "/api/parrot", | ||
method: "POST", | ||
handler: async (ctx: RequestContext) => { | ||
const body = await ctx.request.text(); | ||
return new Response(`Parrot sqwarks: ${body}`); | ||
}, | ||
}); | ||
|
||
export default router; |
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
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
6 changes: 3 additions & 3 deletions
6
examples/preact/src/pages/About.js → example/preactSSR/src/pages/About.ts
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.
aba1aa9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed to deploy: