@@ -4,6 +4,7 @@ import { Hono } from "hono";
44import { renderToString } from "react-dom/server" ;
55import { getShareData } from "./blob" ;
66import { trimTrailingSlash } from "hono/trailing-slash" ;
7+ import { BaseHeader , getAssetPath , HmrScript } from "./utils" ;
78
89// This must be exported for the dev server to work
910export const app = new Hono ( ) ;
@@ -20,6 +21,8 @@ app.route("/api", api);
2021
2122app . use ( trimTrailingSlash ( ) ) ;
2223
24+ app . get ( "/" , ( c ) => c . redirect ( "/parameters" ) ) ;
25+
2326// Serves the main web application. This must come after the API route.
2427app . get ( "/parameters/:shareId?/:example?" , async ( c ) => {
2528 const getExampleCode = async ( ) : Promise < string | null > => {
@@ -37,33 +40,6 @@ app.get("/parameters/:shareId?/:example?", async (c) => {
3740 return examples [ example ] ?? null ;
3841 } ;
3942
40- // Along with the vite React plugin this enables HMR within react while
41- // running the dev server.
42- const { url } = c . req ;
43- const { origin } = new URL ( url ) ;
44- const injectClientScript = `
45- import RefreshRuntime from "${ origin } /@react-refresh";
46- RefreshRuntime.injectIntoGlobalHook(window);
47- window.$RefreshReg$ = () => {};
48- window.$RefreshSig$ = () => (type) => type;
49- window.__vite_plugin_react_preamble_installed__ = true;
50- ` ;
51- const hmrScript = import . meta. env . DEV ? (
52- < script type = "module" > { injectClientScript } </ script >
53- ) : null ;
54-
55- // Sets the correct path for static assets based on the environment.
56- // The production paths are hard coded based on the output of the build script.
57- const cssPath = import . meta. env . PROD
58- ? "/assets/index.css"
59- : "/src/client/index.css" ;
60- const clientScriptPath = import . meta. env . PROD
61- ? "/assets/client.js"
62- : "/src/client/index.tsx" ;
63- const wasmExecScriptPath = import . meta. env . PROD
64- ? "/assets/wasm_exec.js"
65- : "/wasm_exec.js" ;
66- const iconPath = import . meta. env . PROD ? "/assets/logo.svg" : "/logo.svg" ;
6743 const exampleCode = await getExampleCode ( ) ;
6844
6945 return c . html (
@@ -72,28 +48,20 @@ app.get("/parameters/:shareId?/:example?", async (c) => {
7248 renderToString (
7349 < html lang = "en" >
7450 < head >
75- < meta charSet = "UTF-8" />
76- < link rel = "icon" type = "image/svg+xml" href = { iconPath } />
77- < meta
78- name = "viewport"
79- content = "width=device-width, initial-scale=1.0"
80- />
8151 < title > Parameters Playground</ title >
82- < link rel = "stylesheet" href = { cssPath } />
83- { hmrScript }
84- < script type = "module" src = { wasmExecScriptPath } > </ script >
52+ < BaseHeader />
53+ < HmrScript url = { new URL ( c . req . url ) } />
54+ < script type = "module" src = { getAssetPath ( "/wasm_exec.js" ) } > </ script >
8555 </ head >
8656 < body >
8757 < div id = "root" > </ div >
8858 { exampleCode ? (
8959 < script type = "module" > { `window.CODE = ${ JSON . stringify ( exampleCode ) } ` } </ script >
9060 ) : null }
91- < script type = "module" src = { clientScriptPath } > </ script >
61+ < script type = "module" src = { getAssetPath ( "/src/client/index.tsx" ) } > </ script >
9262 </ body >
9363 </ html > ,
9464 ) ,
9565 ] . join ( "\n" ) ,
9666 ) ;
9767} ) ;
98-
99- app . get ( "*" , ( c ) => c . redirect ( "/parameters" ) ) ;
0 commit comments