From e789de1ec693566d0ad8355a74a91a6de2be5870 Mon Sep 17 00:00:00 2001 From: Ryan Carniato Date: Thu, 14 Dec 2023 16:56:43 -0800 Subject: [PATCH] setup modulepreload anticipating vinxi update --- examples/basic/package.json | 2 +- examples/experiments/package.json | 2 +- examples/experiments/src/entry-client.tsx | 10 ++-- examples/experiments/src/routes/(group).tsx | 8 ++++ .../experiments/src/routes/(group)/other.tsx | 3 ++ examples/experiments/src/routes/test.tsx | 10 ++++ examples/experiments/src/routes/test/(hi).tsx | 3 ++ .../experiments/src/routes/test/[name].tsx | 5 ++ examples/hackernews/package.json | 2 +- examples/todomvc/package.json | 2 +- examples/with-auth/package.json | 2 +- examples/with-mdx/package.json | 2 +- examples/with-prisma/package.json | 2 +- examples/with-solid-styled/package.json | 2 +- examples/with-tailwindcss/package.json | 2 +- examples/with-trpc/package.json | 2 +- package.json | 2 +- packages/start/server/StartServer.tsx | 35 +++++++++++++- packages/start/shared/FileRoutes.tsx | 3 +- packages/start/shared/lazyRoute.tsx | 3 +- pnpm-lock.yaml | 48 +++++++++---------- 21 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 examples/experiments/src/routes/(group).tsx create mode 100644 examples/experiments/src/routes/(group)/other.tsx create mode 100644 examples/experiments/src/routes/test.tsx create mode 100644 examples/experiments/src/routes/test/(hi).tsx create mode 100644 examples/experiments/src/routes/test/[name].tsx diff --git a/examples/basic/package.json b/examples/basic/package.json index 8e43edfa6..0f1f6b6b4 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "@solidjs/meta": "^0.29.2", - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "solid-js": "^1.8.7", "vinxi": "0.0.51" diff --git a/examples/experiments/package.json b/examples/experiments/package.json index f381dd4db..e1477bcf1 100644 --- a/examples/experiments/package.json +++ b/examples/experiments/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "@solidjs/meta": "^0.29.2", - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "solid-js": "^1.8.7", "vinxi": "0.0.51" diff --git a/examples/experiments/src/entry-client.tsx b/examples/experiments/src/entry-client.tsx index 30ccc2303..383d81c8e 100644 --- a/examples/experiments/src/entry-client.tsx +++ b/examples/experiments/src/entry-client.tsx @@ -1,10 +1,10 @@ import { mount, StartClient } from "@solidjs/start/client"; -async function hello(name: string) { - "use server"; - console.log("hello", name); -} +// async function hello(name: string) { +// "use server"; +// console.log("hello", name); +// } -await hello("John"); +// await hello("John"); mount(() => , document.getElementById("app")); diff --git a/examples/experiments/src/routes/(group).tsx b/examples/experiments/src/routes/(group).tsx new file mode 100644 index 000000000..cd362dfeb --- /dev/null +++ b/examples/experiments/src/routes/(group).tsx @@ -0,0 +1,8 @@ +import { RouteSectionProps } from "@solidjs/router"; + +export default function(props: RouteSectionProps) { + return <> +

Group

+ {props.children} + +} \ No newline at end of file diff --git a/examples/experiments/src/routes/(group)/other.tsx b/examples/experiments/src/routes/(group)/other.tsx new file mode 100644 index 000000000..ec973e9b2 --- /dev/null +++ b/examples/experiments/src/routes/(group)/other.tsx @@ -0,0 +1,3 @@ +export default function() { + return
OTHER
+} \ No newline at end of file diff --git a/examples/experiments/src/routes/test.tsx b/examples/experiments/src/routes/test.tsx new file mode 100644 index 000000000..c342feaf9 --- /dev/null +++ b/examples/experiments/src/routes/test.tsx @@ -0,0 +1,10 @@ +import { RouteSectionProps } from "@solidjs/router"; + +export default function (props: RouteSectionProps) { + return ( + <> +

Layout

+ {props.children} + + ); +} diff --git a/examples/experiments/src/routes/test/(hi).tsx b/examples/experiments/src/routes/test/(hi).tsx new file mode 100644 index 000000000..e48dba856 --- /dev/null +++ b/examples/experiments/src/routes/test/(hi).tsx @@ -0,0 +1,3 @@ +export default function() { + return
CONTENT
+} \ No newline at end of file diff --git a/examples/experiments/src/routes/test/[name].tsx b/examples/experiments/src/routes/test/[name].tsx new file mode 100644 index 000000000..5a6a3e216 --- /dev/null +++ b/examples/experiments/src/routes/test/[name].tsx @@ -0,0 +1,5 @@ +import { RouteSectionProps } from "@solidjs/router"; + +export default function(props: RouteSectionProps) { + return
{props.params.name}
+} \ No newline at end of file diff --git a/examples/hackernews/package.json b/examples/hackernews/package.json index e54379278..fcdfee83d 100644 --- a/examples/hackernews/package.json +++ b/examples/hackernews/package.json @@ -7,7 +7,7 @@ "start": "node ./.output/server/index.mjs" }, "dependencies": { - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "solid-js": "^1.8.7", "vinxi": "0.0.51" diff --git a/examples/todomvc/package.json b/examples/todomvc/package.json index aaa17e525..bc04e0f47 100644 --- a/examples/todomvc/package.json +++ b/examples/todomvc/package.json @@ -7,7 +7,7 @@ "start": "node ./.output/server/index.mjs" }, "dependencies": { - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "solid-js": "^1.8.7", "unstorage": "1.10.1", diff --git a/examples/with-auth/package.json b/examples/with-auth/package.json index f0f9570f6..9415a9586 100644 --- a/examples/with-auth/package.json +++ b/examples/with-auth/package.json @@ -10,7 +10,7 @@ "@types/node": "^20.10.1" }, "dependencies": { - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "solid-js": "^1.8.7", "vinxi": "0.0.51" diff --git a/examples/with-mdx/package.json b/examples/with-mdx/package.json index ee089723e..318b972f6 100644 --- a/examples/with-mdx/package.json +++ b/examples/with-mdx/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "@mdx-js/mdx": "^2.3.0", - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "@vinxi/plugin-mdx": "^3.6.5", "solid-js": "^1.8.7", diff --git a/examples/with-prisma/package.json b/examples/with-prisma/package.json index ebbb7c020..c50f3e211 100644 --- a/examples/with-prisma/package.json +++ b/examples/with-prisma/package.json @@ -11,7 +11,7 @@ }, "dependencies": { "@prisma/client": "^5.7.0", - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "prisma": "^5.7.0", "solid-js": "^1.8.7", diff --git a/examples/with-solid-styled/package.json b/examples/with-solid-styled/package.json index e5c3485ad..216038b13 100644 --- a/examples/with-solid-styled/package.json +++ b/examples/with-solid-styled/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "@solidjs/meta": "^0.29.2", - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "solid-js": "^1.8.7", "solid-styled": "^0.8.2", diff --git a/examples/with-tailwindcss/package.json b/examples/with-tailwindcss/package.json index d3c369d49..d07283f70 100644 --- a/examples/with-tailwindcss/package.json +++ b/examples/with-tailwindcss/package.json @@ -7,7 +7,7 @@ "start": "node ./.output/server/index.mjs" }, "dependencies": { - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "autoprefixer": "^10.4.14", "postcss": "^8.4.26", diff --git a/examples/with-trpc/package.json b/examples/with-trpc/package.json index 913ef3aff..f2aa7c7f9 100644 --- a/examples/with-trpc/package.json +++ b/examples/with-trpc/package.json @@ -11,7 +11,7 @@ "@trpc/client": "^10.44.1", "@trpc/server": "^10.44.1", "@solidjs/meta": "^0.29.2", - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@solidjs/start": "^0.4.0-alpha.6", "solid-js": "^1.8.7", "valibot": "^0.23.0", diff --git a/package.json b/package.json index da8a86dfe..4c587d696 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.1.0", "@solidjs/meta": "^0.29.0", - "@solidjs/router": "^0.10.2", + "@solidjs/router": "^0.10.3", "@tailwindcss/typography": "^0.5.9", "@trpc/client": "^9.27.4", "@trpc/server": "^9.27.4", diff --git a/packages/start/server/StartServer.tsx b/packages/start/server/StartServer.tsx index fa32215bc..314d9a5c3 100644 --- a/packages/start/server/StartServer.tsx +++ b/packages/start/server/StartServer.tsx @@ -1,7 +1,14 @@ // @ts-ignore import App from "#start/app"; import type { Component } from "solid-js"; -import { Hydration, HydrationScript, NoHydration, getRequestEvent, ssr } from "solid-js/web"; +import { + Hydration, + HydrationScript, + NoHydration, + getRequestEvent, + ssr, + useAssets +} from "solid-js/web"; import { ErrorBoundary } from "../shared/ErrorBoundary"; import { renderAsset } from "./renderAsset"; import type { DocumentComponentProps } from "./types"; @@ -10,6 +17,32 @@ const docType = ssr(""); export function StartServer(props: { document: Component }) { const context = getRequestEvent() as any; + + let assets = []; + Promise.resolve().then(async () => { + let current = context.routes; + if (context.routerMatches[0]) { + for (let i = 0; i < context.routerMatches[0].length; i++) { + const match = context.routerMatches[0][i]; + if (match.metadata && match.metadata.filesystem) { + const segment = current.find(r => r.path === match.path); + const part = import.meta.env.MANIFEST["client"].inputs[segment["$component"].src]; + const asset = await part.assets(); + assets.push.apply(assets, asset); + current = segment.children; + } + } + } + // dedupe assets + assets = [...new Map(assets.map(item => [item.attrs.key, item])).values()].filter( + asset => + asset.attrs.rel === "modulepreload" && + !context.assets.find(a => a.attrs.key === asset.attrs.key) + ); + }); + + useAssets(() => assets.map(m => renderAsset(m))); + return ( {docType as unknown as any} diff --git a/packages/start/shared/FileRoutes.tsx b/packages/start/shared/FileRoutes.tsx index 4bfff8932..a7dce421c 100644 --- a/packages/start/shared/FileRoutes.tsx +++ b/packages/start/shared/FileRoutes.tsx @@ -10,6 +10,7 @@ export function createRoutes() { return { ...route, ...(route.$$route ? route.$$route.require().route : undefined), + metadata: { ...(route.$$route ? route.$$route.require().route.metadata : {}), filesystem: true }, component: lazyRoute( route.$component, import.meta.env.START_ISLANDS @@ -26,5 +27,5 @@ export function createRoutes() { let routes; export const FileRoutes = () => { - return isServer ? (getRequestEvent() as PageEvent).routes : (routes || (routes = createRoutes())); + return isServer ? (getRequestEvent() as PageEvent).routes : routes || (routes = createRoutes()); }; diff --git a/packages/start/shared/lazyRoute.tsx b/packages/start/shared/lazyRoute.tsx index 60d70a3b0..d7bc7b753 100644 --- a/packages/start/shared/lazyRoute.tsx +++ b/packages/start/shared/lazyRoute.tsx @@ -47,9 +47,10 @@ export default function lazyRoute( const mod = await component.import(); const Component = mod[exported]; let assets = await clientManifest.inputs?.[component.src].assets(); + const styles = assets.filter((asset) => asset.tag === "style" || asset.attrs.rel === "stylesheet"); const Comp = (props) => { return [ - ...assets.map((asset) => renderAsset(asset)), + ...styles.map((asset) => renderAsset(asset)), createComponent(Component, props), ]; }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index db5f5b15e..258db9ec2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -28,8 +28,8 @@ importers: specifier: ^0.29.0 version: 0.29.1(solid-js@1.8.7) '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: workspace:* version: link:packages/start @@ -115,8 +115,8 @@ importers: specifier: ^0.29.2 version: 0.29.3(solid-js@1.8.7) '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -133,8 +133,8 @@ importers: specifier: ^0.29.2 version: 0.29.3(solid-js@1.8.7) '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -148,8 +148,8 @@ importers: examples/hackernews: dependencies: '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -163,8 +163,8 @@ importers: examples/todomvc: dependencies: '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -181,8 +181,8 @@ importers: examples/with-auth: dependencies: '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -203,8 +203,8 @@ importers: specifier: ^2.3.0 version: 2.3.0 '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -227,8 +227,8 @@ importers: specifier: ^5.7.0 version: 5.7.0(prisma@5.7.0) '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -252,8 +252,8 @@ importers: specifier: ^0.29.2 version: 0.29.3(solid-js@1.8.7) '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -273,8 +273,8 @@ importers: examples/with-tailwindcss: dependencies: '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -303,8 +303,8 @@ importers: specifier: ^0.29.2 version: 0.29.3(solid-js@1.8.7) '@solidjs/router': - specifier: ^0.10.2 - version: 0.10.2(solid-js@1.8.7) + specifier: ^0.10.3 + version: 0.10.3(solid-js@1.8.7) '@solidjs/start': specifier: ^0.4.0-alpha.6 version: link:../../packages/start @@ -2392,8 +2392,8 @@ packages: dependencies: solid-js: 1.8.7 - /@solidjs/router@0.10.2(solid-js@1.8.7): - resolution: {integrity: sha512-Yp4G9/+oNRTcTQ2snoxsVzo7HZqSm4L5GdaZDIfg24N7wMoRfanQ8aG5DcwEyA5YqFfOF8mjMW3iOCTaafmqdw==} + /@solidjs/router@0.10.3(solid-js@1.8.7): + resolution: {integrity: sha512-UZyUcGbMzeJWCJZthWfveFbv9FyMLi62+BHpXyVwAYpowHaUkkV3WVKoYxYF9m47fVG23UfUo2S6GbuyPAyCjw==} peerDependencies: solid-js: ^1.8.6 dependencies: