From 9ab86510187ca63f0423ea1466bc4b231e9af219 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Wed, 29 Nov 2023 17:44:25 +0100 Subject: [PATCH] feat(server): Add basePath config to host from a directory (#2092) Co-authored-by: Marvin Hagemeister --- docs/canary/concepts/server-configuration.md | 212 ++++++++++++++++++ docs/toc.ts | 2 +- src/build/esbuild.ts | 28 +++ src/dev/dev_command.ts | 5 +- src/server/boot.ts | 5 +- src/server/compose.ts | 5 + src/server/config.ts | 22 ++ src/server/context.ts | 56 +++-- src/server/deps.ts | 6 + src/server/fs_extract.ts | 11 +- src/server/mod.ts | 6 +- src/server/render.ts | 8 +- src/server/rendering/preact_hooks.ts | 43 +++- src/server/rendering/state.ts | 3 + src/server/rendering/template.tsx | 9 +- src/server/router.ts | 7 + src/server/types.ts | 9 + tests/base_path_test.ts | 170 ++++++++++++++ tests/deps.ts | 2 + tests/fixture_base_path/deno.json | 18 ++ tests/fixture_base_path/dev.ts | 5 + tests/fixture_base_path/fresh.gen.ts | 41 ++++ tests/fixture_base_path/islands/Counter.tsx | 22 ++ tests/fixture_base_path/main.ts | 10 + tests/fixture_base_path/routes/_middleware.ts | 11 + tests/fixture_base_path/routes/a/b/b.tsx | 3 + .../routes/a/b/relative/index.tsx | 7 + .../routes/api/base-handler.ts | 9 + tests/fixture_base_path/routes/api/index.ts | 7 + tests/fixture_base_path/routes/api/rewrite.ts | 13 ++ .../routes/baz/[...slug].tsx | 3 + tests/fixture_base_path/routes/codeframe.tsx | 4 + .../routes/error_boundary.tsx | 27 +++ tests/fixture_base_path/routes/html.tsx | 27 +++ tests/fixture_base_path/routes/index.tsx | 3 + tests/fixture_base_path/routes/islands.tsx | 10 + tests/fixture_base_path/static/img.png | Bin 0 -> 4528 bytes tests/fixture_base_path/static/script.js | 1 + tests/fixture_base_path/static/style.css | 3 + tests/test_utils.ts | 5 +- 40 files changed, 810 insertions(+), 28 deletions(-) create mode 100644 docs/canary/concepts/server-configuration.md create mode 100644 tests/base_path_test.ts create mode 100644 tests/fixture_base_path/deno.json create mode 100755 tests/fixture_base_path/dev.ts create mode 100644 tests/fixture_base_path/fresh.gen.ts create mode 100644 tests/fixture_base_path/islands/Counter.tsx create mode 100644 tests/fixture_base_path/main.ts create mode 100644 tests/fixture_base_path/routes/_middleware.ts create mode 100644 tests/fixture_base_path/routes/a/b/b.tsx create mode 100644 tests/fixture_base_path/routes/a/b/relative/index.tsx create mode 100644 tests/fixture_base_path/routes/api/base-handler.ts create mode 100644 tests/fixture_base_path/routes/api/index.ts create mode 100644 tests/fixture_base_path/routes/api/rewrite.ts create mode 100644 tests/fixture_base_path/routes/baz/[...slug].tsx create mode 100644 tests/fixture_base_path/routes/codeframe.tsx create mode 100644 tests/fixture_base_path/routes/error_boundary.tsx create mode 100644 tests/fixture_base_path/routes/html.tsx create mode 100644 tests/fixture_base_path/routes/index.tsx create mode 100644 tests/fixture_base_path/routes/islands.tsx create mode 100644 tests/fixture_base_path/static/img.png create mode 100644 tests/fixture_base_path/static/script.js create mode 100644 tests/fixture_base_path/static/style.css diff --git a/docs/canary/concepts/server-configuration.md b/docs/canary/concepts/server-configuration.md new file mode 100644 index 00000000..77407a20 --- /dev/null +++ b/docs/canary/concepts/server-configuration.md @@ -0,0 +1,212 @@ +--- +description: | + The ability to configure the core Fresh server leads to its flexibility. +--- + +In this page we discuss how the server can be configured during startup. + +The signature of the primary method looks like this: + +```ts main.ts +export async function start(manifest: Manifest, config: FreshConfig = {}); +``` + +## Configuration + +`Manifest` comes from `fresh.gen.ts`, so nothing to do there. `config` is where +things get interesting. +[`FreshConfig`](https://deno.land/x/fresh/server.ts?s=FreshConfig) looks like +this: + +```ts +export interface FreshConfig { + build?: { + /** + * The directory to write generated files to when `dev.ts build` is run. + * This can be an absolute path, a file URL or a relative path. + */ + outDir?: string; + /** + * This sets the target environment for the generated code. Newer + * language constructs will be transformed to match the specified + * support range. See https://esbuild.github.io/api/#target + * @default {"es2022"} + */ + target?: string | string[]; + }; + render?: RenderFunction; + plugins?: Plugin[]; + staticDir?: string; + router?: RouterOptions; + server?: Partial; +} +``` + +And for completeness here are the remaining two types: + +```ts +export type RenderFunction = ( + ctx: RenderContext, + render: InnerRenderFunction, +) => void | Promise; + +export interface RouterOptions { + /** + * Controls whether Fresh will append a trailing slash to the URL. + * @default {false} + */ + trailingSlash?: boolean; + /** + * Configures the pattern of files to ignore in islands and routes. + * + * By default Fresh will ignore test files, + * for example files with a `.test.ts` or a `_test.ts` suffix. + * + * @default {/(?:[^/]*_|[^/]*\.|)test\.(?:ts|tsx|mts|js|mjs|jsx|)\/*$/} + */ + ignoreFilePattern?: RegExp; + /** + * Serve fresh from a base path instead of from the root. + * "/foo/bar" -> http://localhost:8000/foo/bar + * @default {undefined} + */ + basePath?: string; +} +``` + +## Build + +### outDir + +As the comment suggests, this can be used to configure where generated files are +written: + +```tsx +await dev(import.meta.url, "./main.ts", { + build: { + outDir: Deno.env.get("FRESH_TEST_OUTDIR") ?? undefined, + }, +}); +``` + +### target + +This should be a valid ES Build target. + +```tsx +await dev(import.meta.url, "./main.ts", { + build: { + target: "es2015", + }, +}); +``` + +## Plugins + +See the [docs](/docs/concepts/plugins) on this topic for more detail. But as a +quick example, you can do something like this to load plugins: + +```ts main.ts +await start(manifest, { plugins: [twindPlugin(twindConfig)] }); +``` + +## StaticDir + +This allows you to specify the location where your site's static assets are +stored. Here's an example: + +```ts main.ts +await start(manifest, { staticDir: "./custom_static" }); +``` + +## Render + +This is by far the most complicated option currently available. It allows you to +configure how your components get rendered. + +## RouterOptions + +### TrailingSlash + +By default Fresh uses URLs like `https://www.example.com/about`. If you'd like, +you can configure this to `https://www.example.com/about/` by using the +`trailingSlash` setting. + +```ts main.ts +await start(manifest, { router: { trailingSlash: true } }); +``` + +### ignoreFilePattern + +By default Fresh ignores test files which are co-located next routes and +islands. If you want, you can change the pattern Fresh uses ignore these files + +### basePath + +This setting allows you to serve a Fresh app from sub-path of a domain. A value +of `/foo/bar` would serve the app from `http://localhost:8000/foo/bar` instead +of `http://localhost:8000/` for example. + +The `basePath` will be automatically applied to absolute links in your app. For +example, when the `basePath` is `/foo/bar`, linking to `/about` will +automatically become `/foo/bar/about`. + +```jsx +About; +``` + +Rendered HTML: + +```html +About +``` + +The `basePath` is also applied to the `src` and `srcset` attribute of +``-tags, the `href` attribute of `` and the `src` attribute of +` + + + + + MDN + +

css style

+ + + ); +} diff --git a/tests/fixture_base_path/routes/index.tsx b/tests/fixture_base_path/routes/index.tsx new file mode 100644 index 00000000..2f5f11e9 --- /dev/null +++ b/tests/fixture_base_path/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Home() { + return

it works

; +} diff --git a/tests/fixture_base_path/routes/islands.tsx b/tests/fixture_base_path/routes/islands.tsx new file mode 100644 index 00000000..b55c5f70 --- /dev/null +++ b/tests/fixture_base_path/routes/islands.tsx @@ -0,0 +1,10 @@ +import { useSignal } from "@preact/signals"; +import Counter from "../islands/Counter.tsx"; + +export default function Home() { + return ( +
+ +
+ ); +} diff --git a/tests/fixture_base_path/static/img.png b/tests/fixture_base_path/static/img.png new file mode 100644 index 0000000000000000000000000000000000000000..739cc9353258104ff96802ca0006470bf6641725 GIT binary patch literal 4528 zcmb7Ii8~XF19n!UT)8EgiLr&`tQ@ns+02MJLJ32XvpLE!*GS8KGsA2ow?vMT54o>U zlq=0mC_eo9{)FH2yvOst?|<;dqs$F1aENj+F)>{*GK8c5I_*E6JM&j>2aZMlHNYc7 zTR$cyLFxajnk$lD2204#3c0MZ)StVf@;H{75T-L#-jl+(&b*O5cJ3cND z0me8B|NnqoiSyv!Ax_-7T_k9+c?FR*T?FqC%mSDirrsEs#rGKvvDT_Dfg>2qW@F?+$Io8CLZf;1U-lPPKGPg_*H?N|v$ z0zf_=$k*R$T(|DVSapEcR%P~OMejC=lxc1C_v(*RA$=iJnTax8e^01=@C4a># zmuG&Uah3Oh=h zFvIIsRUA%|iygR3={4e`$2%bDp9Avbo)NGt7ejiLStl-%qtzuxt}FdG@U}uXKfET< z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