diff --git a/src/server/fs_extract.ts b/src/server/fs_extract.ts index 63d11714..ef32620a 100644 --- a/src/server/fs_extract.ts +++ b/src/server/fs_extract.ts @@ -296,8 +296,15 @@ export async function extractRoutes( const staticFiles: StaticFile[] = []; try { - const outDirStatic = join(config.build.outDir, "static"); - for (const staticDir of [config.staticDir, outDirStatic]) { + const staticDirs = [config.staticDir]; + + // Only fall through to files in /_fresh/static when not in dev + if (state.loadSnapshot) { + const outDirStatic = join(config.build.outDir, "static"); + staticDirs.push(outDirStatic); + } + + for (const staticDir of staticDirs) { const staticDirUrl = toFileUrl(staticDir); const entries = walk(staticDir, { includeFiles: true, diff --git a/tests/fixture_static/deno.json b/tests/fixture_static/deno.json new file mode 100644 index 00000000..2b2fa78e --- /dev/null +++ b/tests/fixture_static/deno.json @@ -0,0 +1,13 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_static/dev.ts b/tests/fixture_static/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_static/dev.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_static/fresh.gen.ts b/tests/fixture_static/fresh.gen.ts new file mode 100644 index 00000000..28188fcc --- /dev/null +++ b/tests/fixture_static/fresh.gen.ts @@ -0,0 +1,21 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $_app from "./routes/_app.tsx"; +import * as $css from "./routes/css.tsx"; +import * as $index from "./routes/index.tsx"; + +import { type Manifest } from "$fresh/server.ts"; + +const manifest = { + routes: { + "./routes/_app.tsx": $_app, + "./routes/css.tsx": $css, + "./routes/index.tsx": $index, + }, + islands: {}, + baseUrl: import.meta.url, +} satisfies Manifest; + +export default manifest; diff --git a/tests/fixture_static/main.ts b/tests/fixture_static/main.ts new file mode 100644 index 00000000..dedce9cb --- /dev/null +++ b/tests/fixture_static/main.ts @@ -0,0 +1,10 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; + +await start(manifest); diff --git a/tests/fixture_static/routes/_app.tsx b/tests/fixture_static/routes/_app.tsx new file mode 100644 index 00000000..a4b54583 --- /dev/null +++ b/tests/fixture_static/routes/_app.tsx @@ -0,0 +1,17 @@ +import { PageProps } from "$fresh/server.ts"; + +export default function App({ Component }: PageProps) { + return ( + + + + + My Fresh app + + + + + + + ); +} diff --git a/tests/fixture_static/routes/css.tsx b/tests/fixture_static/routes/css.tsx new file mode 100644 index 00000000..fbba9a95 --- /dev/null +++ b/tests/fixture_static/routes/css.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

test

; +} diff --git a/tests/fixture_static/routes/index.tsx b/tests/fixture_static/routes/index.tsx new file mode 100644 index 00000000..d50c5fee --- /dev/null +++ b/tests/fixture_static/routes/index.tsx @@ -0,0 +1,27 @@ +import { basename, dirname, extname, fromFileUrl } from "../../deps.ts"; + +const __dirname = dirname(fromFileUrl(import.meta.url)); + +const links: string[] = []; +for (const file of Deno.readDirSync(__dirname)) { + if (file.name.startsWith("index")) continue; + const name = basename(file.name, extname(file.name)); + links.push(name); +} + +export default function Home() { + return ( +
+

Tests

+
    + {links.sort().map((link) => { + return ( +
  • + {link} +
  • + ); + })} +
+
+ ); +} diff --git a/tests/fixture_static/static/style.css b/tests/fixture_static/static/style.css new file mode 100644 index 00000000..adc68fa6 --- /dev/null +++ b/tests/fixture_static/static/style.css @@ -0,0 +1,3 @@ +h1 { + color: red; +} diff --git a/tests/static_test.ts b/tests/static_test.ts new file mode 100644 index 00000000..2c69700a --- /dev/null +++ b/tests/static_test.ts @@ -0,0 +1,65 @@ +import { assertEquals, dirname, fromFileUrl, join } from "./deps.ts"; +import { withFakeServe } from "./test_utils.ts"; + +Deno.test("don't fallthrough to /_fresh/static in dev", async () => { + const fixtureDir = join( + dirname(fromFileUrl(import.meta.url)), + "fixture_static", + ); + + try { + await Deno.mkdir(join(fixtureDir, "_fresh", "static"), { recursive: true }); + } catch (_err) { + // ignore + } + await Deno.writeTextFile( + join(fixtureDir, "_fresh", "static", "style.css"), + "h1 { color: blue; }", + ); + + await withFakeServe( + "./tests/fixture_static/dev.ts", + async (server) => { + const res = await server.get(`/style.css`); + const css = await res.text(); + assertEquals(css.replace(/\s+/g, ""), "h1{color:red;}"); + }, + ); + + await Deno.remove(join(fixtureDir, "_fresh", "static", "style.css")); + + await withFakeServe( + "./tests/fixture_static/dev.ts", + async (server) => { + const res = await server.get(`/style.css`); + const css = await res.text(); + assertEquals(css.replace(/\s+/g, ""), "h1{color:red;}"); + }, + ); +}); + +Deno.test("fallthrough to /_fresh/static in normal mode", async () => { + const fixtureDir = join( + dirname(fromFileUrl(import.meta.url)), + "fixture_static", + ); + + try { + await Deno.mkdir(join(fixtureDir, "_fresh", "static"), { recursive: true }); + } catch (_err) { + // ignore + } + await Deno.writeTextFile( + join(fixtureDir, "_fresh", "static", "style.css"), + "h1 { color: blue; }", + ); + + await withFakeServe( + "./tests/fixture_static/main.ts", + async (server) => { + const res = await server.get(`/style.css`); + const css = await res.text(); + assertEquals(css.replace(/\s+/g, ""), "h1{color:blue;}"); + }, + ); +});