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 00000000..739cc935 Binary files /dev/null and b/tests/fixture_base_path/static/img.png differ diff --git a/tests/fixture_base_path/static/script.js b/tests/fixture_base_path/static/script.js new file mode 100644 index 00000000..c5ee48e9 --- /dev/null +++ b/tests/fixture_base_path/static/script.js @@ -0,0 +1 @@ +document.querySelector("#script-output").textContent = "it works"; diff --git a/tests/fixture_base_path/static/style.css b/tests/fixture_base_path/static/style.css new file mode 100644 index 00000000..a15c877a --- /dev/null +++ b/tests/fixture_base_path/static/style.css @@ -0,0 +1,3 @@ +.foo { + color: red; +} diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 21f2fb9b..5abb3eb9 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -235,7 +235,6 @@ export async function withPageName( } } finally { serverProcess.kill("SIGTERM"); - // Wait until the process exits await serverProcess.status; @@ -483,7 +482,9 @@ async function spawnServer( // @ts-ignore yes it does for await (const line of lines.values({ preventCancel: true })) { output.push(line); - const match = line.match(/https?:\/\/localhost:\d+/g); + const match = line.match( + /https?:\/\/localhost:\d+(\/\w+[-\w]*)*/g, + ); if (match) { address = match[0]; break;