+
+Try out the [**alpha release** of Fastify DX for Svelte](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).
+
+
+
+
+
+
+
## Status
Fastify DX is currently in **alpha**.
diff --git a/docs/svelte/basic-setup.md b/docs/svelte/basic-setup.md
new file mode 100644
index 0000000..9137bab
--- /dev/null
+++ b/docs/svelte/basic-setup.md
@@ -0,0 +1,87 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Basic setup
+
+The [starter template](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte) follows [fastify-vite](https://github.com/fastify/fastify-vite)'s convention of having a `client` folder with an `index.js` file, which is automatically resolved as your `clientModule` setting.
+
+If you want flat directory setup, where server and client files are mixed together, you can manually set `clientModule` to something else. Note that in this case you'll also need to update `root` in your `vite.config.js` file.
+
+When deploying to production, bear in mind the `client/dist` directory, generated when you run `npm run build`, needs to be included. You'll also want to enable Fastify's [built-in logging](https://www.fastify.io/docs/latest/Reference/Logging/):
+
+```js
+const server = Fastify({ logger: true })
+```
+
+The starter template's `server.js` file:
+
+```js
+import Fastify from 'fastify'
+import FastifyVite from 'fastify-vite'
+import FastifyDXSvelte from 'fastify-dx-svelte'
+
+const server = Fastify()
+
+await server.register(FastifyVite, {
+ root: import.meta.url,
+ renderer: FastifyDXSvelte,
+})
+
+await server.vite.ready()
+await server.listen(3000)
+```
+
+The starter template's [`vite.config.js`](https://github.com/fastify/fastify-dx/blob/main/starters/svelte/vite.config.js) file:
+
+```js
+import { join, dirname } from 'path'
+import { fileURLToPath } from 'url'
+
+import { svelte as viteSvelte } from '@sveltejs/vite-plugin-svelte'
+import viteSvelteFastifyDX from 'fastify-dx-svelte/plugin'
+import unocss from 'unocss/vite'
+import { extractorSvelte } from '@unocss/core'
+
+const path = fileURLToPath(import.meta.url)
+
+const root = join(dirname(path), 'client')
+const plugins = [
+ unocss({ extractors: [extractorSvelte] }),
+ viteSvelte({
+ compilerOptions: {
+ hydratable: true,
+ }
+ }),
+ viteSvelteFastifyDX(),
+]
+
+export default { root, plugins }
+```
+
+Note that you only need to use Fastify DX's Vite plugin, which includes all functionality from [fastify-vite](https://github.com/fastify/fastify-vite)'s Vite plugin.
+
+
+
+
+
+### Route exports
+
+Fastify DX picks up exports from route modules to determine route behavior and functionality, as per the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md).
+
+To add those exports, you must use `
+
+
{data.message}
+```
diff --git a/docs/svelte/data-prefetching.md b/docs/svelte/data-prefetching.md
new file mode 100644
index 0000000..d010712
--- /dev/null
+++ b/docs/svelte/data-prefetching.md
@@ -0,0 +1,30 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Isomorphic data prefetching
+
+Fastify DX for Svelte implements the `getData()` hook from the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md) to solve this problem.
+
+### `getData(ctx)`
+
+This hook is set up in a way that it runs server-side before any SSR takes place, so any data fetched is made available to the route component before it starts rendering. During first render, any data retrieved on the server is automatically sent to be hydrated on the client so no new requests are made. Then, during client-side navigation (post first-render), a JSON request is fired to an endpoint automatically registered for running the `getData()` function for that route on the server.
+
+The objet returned by `getData()` gets automatically assigned as `data` in the [universal route context](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-context.md) object and is accessible from `getMeta()` and `onEnter()` hooks and also via the `useRouteContext()` hook.
+
+```html
+
+
+
+
+
{data.message}
+```
diff --git a/docs/svelte/index.md b/docs/svelte/index.md
new file mode 100644
index 0000000..b54efcf
--- /dev/null
+++ b/docs/svelte/index.md
@@ -0,0 +1,94 @@
+# fastify-dx-svelte [![NPM version](https://img.shields.io/npm/v/fastify-dx-svelte.svg?style=flat)](https://www.npmjs.com/package/fastify-dx-svelte) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
+
+- [**Introduction**](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md#introduction)
+- [**Quick Start**](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md#quick-start)
+- [**Package Scripts**](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md#package-scripts)
+- [**Basic Setup**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/basic-setup.md)
+- [**Project Structure**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/project-structure.md)
+- [**Rendering Modes**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/rendering-modes.md)
+- [**Routing Configuration**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/routing-config.md)
+- [**Data Prefetching**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/data-prefetching.md)
+- [**Route Layouts**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-layouts.md)
+- [**Route Context**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-context.md)
+- [**Route Enter Event**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-enter.md)
+- [**Virtual Modules**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/virtual-modules.md)
+
+## Introduction
+
+**Fastify DX for Svelte** is a renderer adapter for [**fastify-vite**](https://github.com/fastify/fastify-vite).
+
+It lets you run and SSR (server-side render) **Svelte applications built with Vite** on [Fastify](https://fastify.io/), with a minimal and transparent **server-first approach** — everything starts with `server.js`, your actual Fastify server).
+
+It also provides a set of built-in utilities for ease of development and managing a universal JavaScript context (SSR to CSR), very much like **Nuxt.js**, **Next.js** and **Remix**. All **Fastify DX** framework adapters implement the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md) and have almost the same API, with only minimal differences due to specific framework APIs or idioms.
+
+It is a **fast**, **lightweight** alternative to Nuxt.js packed with **Developer Experience** features.
+
+It has an extremely small core (~1k LOC total) and is built on top of [Fastify](https://github.com/fastify/fastify), [Vite](https://vitejs.dev/), [Valtio](https://github.com/pmndrs/valtio) and [Svelte Routing](https://github.com/EmilTholin/svelte-routing).
+
+[**See the release notes for the 0.0.1 alpha release**](https://github.com/fastify/fastify-dx/releases/tag/svelte-v0.0.1).
+
+> At this stage this project is mostly a [**one-man show**](https://github.com/sponsors/galvez), who's devoting all his free time to its completion. Contributions are extremely welcome, as well as bug reports for any issues you may find.
+
+In this first alpha release it's still missing a test suite. The same is true for [**fastify-vite**](https://github.com/fastify/fastify-vite).
+
+It'll move into **beta** status when test suites are added to both packages.
+
+## Quick Start
+
+Ensure you have **Node v16+**.
+
+Make a copy of [**starters/svelte**](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte). If you have [`degit`](https://github.com/Rich-Harris/degit), run the following from a new directory:
+
+```bash
+degit fastify/fastify-dx/starters/svelte
+```
+
+> **If you're starting a project from scratch**, you'll need these packages installed.
+>
+> ```bash
+> npm i fastify fastify-vite fastify-dx-svelte -P
+> npm i @sveltejs/vite-plugin-svelte -D
+> ```
+
+
+Run `npm install`.
+
+Run `npm run dev`.
+
+Visit `http://localhost:3000/`.
+
+## What's Included
+
+That will get you a **starter template** with:
+
+- A minimal [Fastify](https://github.com/fastify/fastify) server.
+- Some dummy API routes.
+- A `pages/` folder with some [demo routes](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte/client/pages).
+- All configuration files.
+
+It also includes some _**opinionated**_ essentials:
+
+- [**PostCSS Preset Env**](https://www.npmjs.com/package/postcss-preset-env) by [**Jonathan Neal**](https://github.com/jonathantneal), which enables [several modern CSS features](https://preset-env.cssdb.org/), such as [**CSS Nesting**](https://www.w3.org/TR/css-nesting-1/).
+
+- [**UnoCSS**](https://github.com/unocss/unocss) by [**Anthony Fu**](https://antfu.me/), which supports all [Tailwind utilities](https://uno.antfu.me/) and many other goodies through its [default preset](https://github.com/unocss/unocss/tree/main/packages/preset-uno).
+
+- [**Valtio**](https://github.com/pmndrs/valtio) by [**Daishi Kato**](https://blog.axlight.com/), with a global and SSR-ready store which you can use anywhere. Svelte support is provided via [Sveltio](https://github.com/wobsoriano/sveltio) by [Robert Soriano](https://robsoriano.com/).
+
+
+## Package Scripts
+
+`npm run dev` boots the development server.
+
+`npm run build` creates the production bundle.
+
+`npm run serve` serves the production bundle.
+
+## Meta
+
+Created by [Jonas Galvez](https://github.com/sponsors/galvez), **Engineering Manager** and **Open Sourcerer** at [NearForm](https://nearform.com).
+
+## Sponsors
+
+
+
+Also [**Duc-Thien Bui**](https://github.com/aecea) and [**Tom Preston-Werner**](https://github.com/mojombo) [via GitHub Sponsors](https://github.com/sponsors/galvez). _Thank you!_
diff --git a/docs/svelte/meta-tags.md b/docs/svelte/meta-tags.md
new file mode 100644
index 0000000..d406447
--- /dev/null
+++ b/docs/svelte/meta-tags.md
@@ -0,0 +1,30 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Meta Tags
+
+Following the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md), Fastify DX renders `` elements independently from the SSR phase. This allows you to fetch data for populating the first `` tags and stream them right away to the client, and only then perform SSR.
+
+> Additional `` preload tags can be produced from the SSR phase. This is **not currently implemented** in this **alpha release** but is a planned feature. If you can't wait for it, you can roll out your own (and perhaps contribute your solution) by providing your own [`createHtmlFunction()`](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/index.js#L57) to [fastify-vite](https://github.com/fastify/fastify-vite).
+
+### `getMeta()`
+
+To populate ``, `` and `` elements, export a `getMeta()` function that returns an object matching the format expected by [unihead](https://github.com/galvez/unihead), the underlying library used by Fastify DX.
+
+It receives the [route context](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md#route-context) as first parameter and runs after `getData()`, allowing you to access any `data` populated by these other functions to generate your tags.
+
+```html
+
+
+
Route with meta tags.
+```
diff --git a/docs/svelte/project-structure.md b/docs/svelte/project-structure.md
new file mode 100644
index 0000000..dc3132c
--- /dev/null
+++ b/docs/svelte/project-structure.md
@@ -0,0 +1,61 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Project Structure
+
+The [starter template](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte) looks like this:
+
+```
+├── server.js
+├── client/
+│ ├── index.js
+│ ├── context.js
+│ ├── root.svelte
+│ ├── index.html
+│ ├── layouts/
+│ │ ├── default.svelte
+│ │ └── auth.svelte
+│ └── pages/
+│ ├── index.svelte
+│ ├── client-only.svelte
+│ ├── server-only.svelte
+│ ├── using-data.svelte
+│ └── using-store.svelte
+├── vite.config.js
+└── package.json
+```
+
+Several internal files are provided as virtual modules by Fastify DX. They are located inside the `fastify-dx-svelte` package in `node_modules`, and dynamically loaded so you don't have to worry about them unless you want them overriden.
+
+In this case, placing a file with the same name as the registered virtual module in your Vite project root will override it. Find the detailed rundown of all virtual modules [here][virtual-modules].
+
+[virtual-modules]: https://github.com/fastify/fastify-dx/blob/main/docs/svelte/virtual-modules.md
+
+The `server.js` file is your application entry point. It's the file that runs everything. It boots a Fastify server configured with [**fastify-vite**](https://github.com/fastify/fastify-vite) and **Fastify DX for Svelte** as a renderer adapter to **fastify-vite**.
+
+The `client/index.js` file is your Vite server entry point, it's the file that provides your client bundle (which runs in the Vite-enriched environment) to the Node.js environment where Fastify runs.
+
+> Right now, it's mostly a **boilerplate file** because it must exist but it will also probably never need to be changed.
+
+It exports your application's factory function (must be named `create`), the application routes (must be named `routes`) and the universal route context [initialization module](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-context.md#initialization-module) (must be named `context` and have a dynamic module import so Fastify DX can pick up `default` and named exports).
+
+The `client/index.html` file is the [root HTML template of the application](https://vitejs.dev/guide/#index-html-and-project-root), which Vite uses as the client bundling entry point.
+
+> You can expand this file with additional `` and `` tags if you wish, provided you don't remove any of the placeholders.
+
+This files links to `/dx:mount.js`, which is a virtual module provided by Fastify DX.
+
+Virtual modules are covered [here][virtual-modules].
+
+The `client/pages/` directory contains your route modules, whose paths are dynamically inferred from the directory structure itself. You can change this behavior easily. More on this [here][routing-config].
+
+[routing-config]: https://github.com/fastify/fastify-dx/blob/main/docs/svelte/routing-config.md
+
+The `client/layouts/` directory contains your route layout modules, which can be associated to any route. By default, `layouts/default.svelte` is used, but if you don't need to do any modifications on that file, you can safely removed as it's provided by Fastify DX in that case. The starter template also comes with `layouts/auth.svelte`, to demonstrate a more advanced use of layouts.
+
+[routing-config]: https://github.com/fastify/fastify-dx/blob/main/docs/svelte/routing-config.md
+
+The `client/context.js` file is the universal [route context][route-context] initialization module. Any named exports from this file are attached to the `RouteContext` class prototype on the server, preventing them from being reassigned on every request. The `default` export from this file, however, runs for every request so you can attach any request-specific data to it.
+
+[route-context]: https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-context.md
diff --git a/docs/svelte/rendering-modes.md b/docs/svelte/rendering-modes.md
new file mode 100644
index 0000000..8f2a237
--- /dev/null
+++ b/docs/svelte/rendering-modes.md
@@ -0,0 +1,39 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+# Rendering modes
+
+Following the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md), Fastify DX's route modules can be set for universal rendering (SSR + CSR hydration, the default behavior), SSR in streaming mode, SSR only (client gets no JavaScript) or CSR only (SSR fully disabled). Fastify DX for Svelte supports all of these modes minus streaming, which is currently not yet supported by Svelte itself.
+
+## `serverOnly`
+
+If a route module exports `serverOnly` set to `true`, only SSR will take place. The client gets the server-side rendered markup without any accompanying JavaScript or data hydration.
+
+You should use this setting to deliver lighter pages when there's no need to run any code on them, such as statically generated content sites.
+
+```html
+
+
+
This route is rendered on the server only!
+```
+
+[This example](https://github.com/fastify/fastify-dx/blob/main/starters/svelte/client/pages/server-only.svelte) is part of the [starter template](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte).
+
+## `clientOnly`
+
+If a route module exports `clientOnly` set to `true`, no SSR will take place, only data fetching and data hydration. The client gets the empty container element (the one that wraps `` in `index.html`) and all rendering takes place on the client only.
+
+You can use this setting to save server resources on internal pages where SSR makes no significant diference for search engines or UX in general, such as a password-protected admin section.
+
+```html
+
+
+
This route is rendered on the client only!
+```
+
+[This example](https://github.com/fastify/fastify-dx/blob/main/starters/svelte/client/pages/client-only.svelte) is part of the [starter template](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte).
diff --git a/docs/svelte/route-context.md b/docs/svelte/route-context.md
new file mode 100644
index 0000000..cb152f1
--- /dev/null
+++ b/docs/svelte/route-context.md
@@ -0,0 +1,95 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Route Context
+
+### Initialization module
+
+The starter template includes a sample `context.js` file. This file is optional and can be safely removed. If it's present, Fastify DX automatically loads it and uses it to do any RouteContext extensions or data injections you might need. If you're familiar with [Nuxt.js](https://nuxtjs.org/), you can think of it as a [Nuxt.js plugin](https://nuxtjs.org/docs/directory-structure/plugins/).
+
+**Consuming the route context:**
+
+```js
+import {
+ useRouteContext
+} from '/dx:core.js'
+
+// ...
+const {
+ state,
+ actions
+} = useRouteContext()
+
+// ...
+actions.addTodoItem(state, value)
+```
+
+See the [full example](https://github.com/fastify/fastify-dx/blob/main/starters/svelte/client/pages/using-store.vue) in the starter template.
+
+This example demonstrates how to use it to set up an universally available (SSR and CSR) `$fetch` function (using [`ky-universal`](https://www.npmjs.com/package/ky-universal)) and also export some store actions. They're all made available by `useRouteContext()`, covered next.
+
+```js
+import ky from 'ky-universal'
+
+export default (ctx) => {
+ if (ctx.server) {
+ // Populate state.todoList on the server
+ ctx.state.todoList = ctx.server.db.todoList
+ // It'll get automatically serialized to the client on first render!
+ }
+}
+
+export const $fetch = ky.extend({
+ prefixUrl: 'http://localhost:3000'
+})
+
+// Must be a function so each request can have its own state
+export const state = () => ({
+ todoList: null,
+})
+
+export const actions = {
+ async addTodoItem (state, item) {
+ await $fetch.put('api/todo/items', {
+ json: { item },
+ })
+ state.todoList.push(item)
+ },
+}
+```
+
+See the [full example](https://github.com/fastify/fastify-dx/blob/main/starters/svelte/client/context.js) in the starter template.
+
+### The `useRouteContext()` hook
+
+This hook can be used in any Vue component to retrieve a reference to the current route context. It's modelled after the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md), with still some rough differences and missing properties in this **alpha release**.
+
+By default, It includes reference to `data` — which is automatically populated if you use the `getData()` function, and `state` which hold references to the global [`reactive()`](https://vuejs.org/api/reactivity-core.html#reactive) object.
+
+It automatically causes the component to be suspended if the `getData()`, `getMeta()` and `onEnter()` functions are asynchronous.
+
+```html
+
+
+
{data.message}
+```
+
+### Execution order
+
+This graph illustrates the execution order to expect from route context initialization.
+
+```
+context.js default function export
+└─ getData() function export
+ └─ getMeta() function export
+ └─ onEnter() function export
+ └─ Route module
+```
+
+First the `default` function export from `context.js` (if present) is executed. This is where you can manually feed global server data into your application by populating the global state (the route context's `state` property, which is automatically hydrated on the client.
+
+Then `getData()` runs — which populates the route context's `data` property, and is also automatically hydrated on the client. Then `getMeta()`, which populates the route context's `head` property. Then `onEnter()`, and finally your route component.
diff --git a/docs/svelte/route-enter.md b/docs/svelte/route-enter.md
new file mode 100644
index 0000000..84abad8
--- /dev/null
+++ b/docs/svelte/route-enter.md
@@ -0,0 +1,27 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Universal Route Enter Event
+
+### `onEnter(ctx)`
+
+If a route module exports a `onEnter()` function, it's executed before the route renders, both in SSR and client-side navigation. That is, the first time a route render on the server, onEnter() runs on the server. Then, since it already ran on the server, it doesn't run again on the client for that first route. But if you navigate to another route on the client using ``, it runs normally as you'd expect.
+
+It receives the [universal route context][route-context] as first parameter, so you can make changes to `data`, `meta` and `state` if needed.
+
+[route-context]: https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-context.md
+
+```html
+
+
+
No pre-rendered HTML sent to the browser.
+```
+
+The example demonstrates how to turn off SSR and downgrade to CSR-only, assuming you have a `pressureHandler` configured in [`underpressure`](https://github.com/fastify/under-pressure) to set a `underPressure` flag on your server instance.
diff --git a/docs/svelte/route-layouts.md b/docs/svelte/route-layouts.md
new file mode 100644
index 0000000..7f386d5
--- /dev/null
+++ b/docs/svelte/route-layouts.md
@@ -0,0 +1,39 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Route Layouts
+
+Fastify DX will automatically load layouts from the `layouts/` folder. By default, `/dx:layouts/default.svelte` is used — that is, if a project is missing a `layouts/defaults.svelte` file, the one provided by Fastify DX is used instead.
+
+See the section on [Virtual Modules](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/virtual-modules.md) to learn more about this.
+
+You assign a layout to a route by exporting `layout`.
+
+See [`pages/using-auth.svelte`](https://github.com/fastify/fastify-dx/blob/main/starters/svelte/pages/using-auth.svelte) in the starter template:
+
+```js
+export const layout = 'auth'
+```
+
+That'll will cause the route to be wrapped in the layout component exported by [`layouts/auth.svelte`](https://github.com/fastify/fastify-dx/blob/main/starters/svelte/layouts/auth.svelte):
+
+```html
+
+
+
+ {#if !$snapshot.user.authenticated}
+
This route needs authentication.
+
+ {:else}
+
+ {/if}
+
+```
+
+Note that like routes, it has access to `useRouteContext()`.
diff --git a/docs/svelte/routing-config.md b/docs/svelte/routing-config.md
new file mode 100644
index 0000000..6a170ff
--- /dev/null
+++ b/docs/svelte/routing-config.md
@@ -0,0 +1,47 @@
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Routing Configuration
+
+By default, routes are loaded from the `/pages` folder, where `` refers to the `root` setting in `vite.config.js`. The route paths are **dynamically inferred from the directory structure**, very much like Next.js and Nuxt.js.
+
+### Dynamic parameters
+
+Dynamic route parameters follow the [Next.js convention](https://nextjs.org/docs/basic-features/pages#pages-with-dynamic-routes) (`[param]`), but that can be overriden by using the `paramPattern` plugin option. For example, this configuration switches the param pattern to the [Remix convention](https://remix.run/docs/en/v1/guides/routing#dynamic-segments) (`$param`).
+
+```js
+// ...
+const plugins = [
+ // ...
+ viteSvelteFastifyDX({ paramPattern: /\$(\w+)/ }),
+]
+```
+
+### Routes location
+
+You can also change the glob pattern used to determine where to route modules from.
+
+Since this setting is passed to [Vite's glob importers](https://vitejs.dev/guide/features.html#glob-import), the value needs to be a string:
+
+```js
+// ...
+const plugins = [
+ // ...
+ viteSvelteFastifyDX({ globPattern: '/views/**/*.svelte' }),
+]
+```
+
+### View modules
+
+You also can export a `path` constant from your route modules, in which case its value will be used to **override the dynamically inferred paths from the directory structure**.
+
+Additionally, [**you can provide your own routes**](https://github.com/fastify/fastify-dx/tree/dev/packages/fastify-dx-svelte#dxroutesjs).
+
+```html
+
+
+
Route with path export
+```
diff --git a/docs/svelte/virtual-modules.md b/docs/svelte/virtual-modules.md
new file mode 100644
index 0000000..1ba5dbb
--- /dev/null
+++ b/docs/svelte/virtual-modules.md
@@ -0,0 +1,206 @@
+
+**Go back to the [index](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md).**
+
+
+
+## Virtual Modules
+
+**Fastify DX** relies on [virtual modules](https://github.com/rollup/plugins/tree/master/packages/virtual) to save your project from having too many boilerplate files. Virtual modules are a [Rollup](https://rollupjs.org/guide/en/) feature exposed and fully supported by [Vite](https://vitejs.dev/). When you see imports that start with `/dx:`, you know a Fastify DX virtual module is being used.
+
+Fastify DX virtual modules are **fully ejectable**. For instance, the starter template relies on the `/dx:root.svelte` virtual module to provide the Vue shell of your application. If you copy the `root.svelte` file [from the fastify-dx-svelte package](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/virtual/root.svelte) and place it your Vite project root, **that copy of the file is used instead**. In fact, the starter template already comes with a custom `root.svelte` of its own to include UnoCSS.
+
+Aside from `root.svelte`, the starter template comes with two other virtual modules already ejected and part of the local project — `context.js` and `layouts/default.svelte`. If you don't need to customize them, you can safely removed them from your project.
+
+### `/dx:root.svelte`
+
+This is the root Svelte component. It's provided as part of the starter template. You can use this file to add a common layout to all routes. The version provided as part of the starter template includes [UnoCSS](https://github.com/unocss/unocss)'s own virtual module import, necessary to enable its CSS engine.
+
+```html
+
+
+
+ {#each payload.routes as { path, component }}
+
+
+
+ {/each}
+
+```
+
+### `/dx:route.svelte`
+
+This is used by `root.svelte` to enhance your route modules with the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md).
+
+You'll rarely need to customize this file.
+
+```html
+
+
+{#if isServer}
+
+
+
+{:else}
+{#await setupClientRouteContext}{:then}
+
+
+
+{/await}
+{/if}
+```
+
+What you see above is its [full definition](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/virtual/route.svelte).
+
+
+### `/dx:routes.js`
+
+Fastify DX has **code-splitting** out of the box. It does that by eagerly loading all route data on the server, and then hydrating any missing metadata on the client. That's why the routes module default export is conditioned to `import.meta.env.SSR`, and different helper functions are called for each rendering environment.
+
+```js
+export default import.meta.env.SSR
+ ? createRoutes(import.meta.globEager('$globPattern'))
+ : hydrateRoutes(import.meta.glob('$globPattern'))
+```
+
+See [the full file](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/virtual/routes.js) for the `createRoutes()` and `hydrateRoutes()` definitions.
+
+If you want to use your own custom routes list, you must eject this file as-is and replace the glob imports with your own routes list:
+
+```js
+const routes = [
+ {
+ path: '/',
+ component: () => import('/custom/index.svelte'),
+ }
+]
+
+export default import.meta.env.SSR
+ ? createRoutes(routes)
+ : hydrateRoutes(routes)
+````
+
+**Nested routes aren't supported yet.**
+
+
+### `/dx:core.js`
+
+Implements `useRouteContext()`.
+
+See its full definition [here](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/virtual/core.js).
+
+### `/dx:layouts.js`
+
+This is responsible for loading **layout components**. It's part of `route.svelte` by default. If a project has no `layouts/default.svelte` file, the default one from Fastify DX is used. This virtual module works in conjunction with the `/dx:layouts/` virtual module which provides exports from the `/layouts` folder.
+
+You'll rarely need to customize this file.
+
+```js
+import DefaultLayout from '/dx:layouts/default.svelte'
+
+const appLayouts = import.meta.globEager('/layouts/*.svelte')
+
+appLayouts['/layouts/default.svelte'] ??= DefaultLayout
+
+export default Object.fromEntries(
+ Object.keys(appLayouts).map((path) => {
+ const name = path.slice(9, -7)
+ return [name, appLayouts[path]]
+ }),
+)
+
+```
+
+What you see above is its [full definition](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/virtual/layouts.js).
+
+### `/dx:mount.js`
+
+This is the file `index.html` links to by default. It sets up the application with an `unihead` instance for head management, the initial route context, and provides the conditional mounting logic to defer to CSR-only if `clientOnly` is enabled.
+
+You'll rarely need to customize this file.
+
+[See the full file](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/virtual/mount.js) for the `mount()` function definition.
diff --git a/packages/fastify-dx-svelte/.eslintrc b/packages/fastify-dx-svelte/.eslintrc
new file mode 100644
index 0000000..e5461ce
--- /dev/null
+++ b/packages/fastify-dx-svelte/.eslintrc
@@ -0,0 +1,21 @@
+{
+ parser: '@babel/eslint-parser',
+ parserOptions: {
+ requireConfigFile: false,
+ ecmaVersion: 2021,
+ sourceType: 'module',
+ },
+ plugins: [
+ 'svelte3'
+ ],
+ overrides: [
+ {
+ files: ['*.svelte'],
+ processor: 'svelte3/svelte3'
+ }
+ ],
+ rules: {
+ 'comma-dangle': ['error', 'always-multiline'],
+ 'import/no-absolute-path': 'off',
+ },
+}
diff --git a/packages/fastify-dx-svelte/README.md b/packages/fastify-dx-svelte/README.md
new file mode 100644
index 0000000..b54efcf
--- /dev/null
+++ b/packages/fastify-dx-svelte/README.md
@@ -0,0 +1,94 @@
+# fastify-dx-svelte [![NPM version](https://img.shields.io/npm/v/fastify-dx-svelte.svg?style=flat)](https://www.npmjs.com/package/fastify-dx-svelte) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
+
+- [**Introduction**](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md#introduction)
+- [**Quick Start**](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md#quick-start)
+- [**Package Scripts**](https://github.com/fastify/fastify-dx/blob/main/packages/fastify-dx-svelte/README.md#package-scripts)
+- [**Basic Setup**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/basic-setup.md)
+- [**Project Structure**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/project-structure.md)
+- [**Rendering Modes**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/rendering-modes.md)
+- [**Routing Configuration**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/routing-config.md)
+- [**Data Prefetching**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/data-prefetching.md)
+- [**Route Layouts**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-layouts.md)
+- [**Route Context**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-context.md)
+- [**Route Enter Event**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/route-enter.md)
+- [**Virtual Modules**](https://github.com/fastify/fastify-dx/blob/main/docs/svelte/virtual-modules.md)
+
+## Introduction
+
+**Fastify DX for Svelte** is a renderer adapter for [**fastify-vite**](https://github.com/fastify/fastify-vite).
+
+It lets you run and SSR (server-side render) **Svelte applications built with Vite** on [Fastify](https://fastify.io/), with a minimal and transparent **server-first approach** — everything starts with `server.js`, your actual Fastify server).
+
+It also provides a set of built-in utilities for ease of development and managing a universal JavaScript context (SSR to CSR), very much like **Nuxt.js**, **Next.js** and **Remix**. All **Fastify DX** framework adapters implement the [URMA specification](https://github.com/fastify/fastify-dx/blob/main/URMA.md) and have almost the same API, with only minimal differences due to specific framework APIs or idioms.
+
+It is a **fast**, **lightweight** alternative to Nuxt.js packed with **Developer Experience** features.
+
+It has an extremely small core (~1k LOC total) and is built on top of [Fastify](https://github.com/fastify/fastify), [Vite](https://vitejs.dev/), [Valtio](https://github.com/pmndrs/valtio) and [Svelte Routing](https://github.com/EmilTholin/svelte-routing).
+
+[**See the release notes for the 0.0.1 alpha release**](https://github.com/fastify/fastify-dx/releases/tag/svelte-v0.0.1).
+
+> At this stage this project is mostly a [**one-man show**](https://github.com/sponsors/galvez), who's devoting all his free time to its completion. Contributions are extremely welcome, as well as bug reports for any issues you may find.
+
+In this first alpha release it's still missing a test suite. The same is true for [**fastify-vite**](https://github.com/fastify/fastify-vite).
+
+It'll move into **beta** status when test suites are added to both packages.
+
+## Quick Start
+
+Ensure you have **Node v16+**.
+
+Make a copy of [**starters/svelte**](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte). If you have [`degit`](https://github.com/Rich-Harris/degit), run the following from a new directory:
+
+```bash
+degit fastify/fastify-dx/starters/svelte
+```
+
+> **If you're starting a project from scratch**, you'll need these packages installed.
+>
+> ```bash
+> npm i fastify fastify-vite fastify-dx-svelte -P
+> npm i @sveltejs/vite-plugin-svelte -D
+> ```
+
+
+Run `npm install`.
+
+Run `npm run dev`.
+
+Visit `http://localhost:3000/`.
+
+## What's Included
+
+That will get you a **starter template** with:
+
+- A minimal [Fastify](https://github.com/fastify/fastify) server.
+- Some dummy API routes.
+- A `pages/` folder with some [demo routes](https://github.com/fastify/fastify-dx/tree/dev/starters/svelte/client/pages).
+- All configuration files.
+
+It also includes some _**opinionated**_ essentials:
+
+- [**PostCSS Preset Env**](https://www.npmjs.com/package/postcss-preset-env) by [**Jonathan Neal**](https://github.com/jonathantneal), which enables [several modern CSS features](https://preset-env.cssdb.org/), such as [**CSS Nesting**](https://www.w3.org/TR/css-nesting-1/).
+
+- [**UnoCSS**](https://github.com/unocss/unocss) by [**Anthony Fu**](https://antfu.me/), which supports all [Tailwind utilities](https://uno.antfu.me/) and many other goodies through its [default preset](https://github.com/unocss/unocss/tree/main/packages/preset-uno).
+
+- [**Valtio**](https://github.com/pmndrs/valtio) by [**Daishi Kato**](https://blog.axlight.com/), with a global and SSR-ready store which you can use anywhere. Svelte support is provided via [Sveltio](https://github.com/wobsoriano/sveltio) by [Robert Soriano](https://robsoriano.com/).
+
+
+## Package Scripts
+
+`npm run dev` boots the development server.
+
+`npm run build` creates the production bundle.
+
+`npm run serve` serves the production bundle.
+
+## Meta
+
+Created by [Jonas Galvez](https://github.com/sponsors/galvez), **Engineering Manager** and **Open Sourcerer** at [NearForm](https://nearform.com).
+
+## Sponsors
+
+
+
+Also [**Duc-Thien Bui**](https://github.com/aecea) and [**Tom Preston-Werner**](https://github.com/mojombo) [via GitHub Sponsors](https://github.com/sponsors/galvez). _Thank you!_
diff --git a/packages/fastify-dx-svelte/index.js b/packages/fastify-dx-svelte/index.js
index e69de29..3d8239e 100644
--- a/packages/fastify-dx-svelte/index.js
+++ b/packages/fastify-dx-svelte/index.js
@@ -0,0 +1,186 @@
+// Used to send a readable stream to reply.send()
+import { Readable } from 'stream'
+
+// fastify-vite's minimal HTML templating function,
+// which extracts interpolation variables from comments
+// and returns a function with the generated code
+import { createHtmlTemplateFunction } from 'fastify-vite'
+
+// Used to safely serialize JavaScript into
+// '
+ )
+ }
+ // Render page-level elements
+ const head = new Head(context.head).render()
+ const style = (
+ app.style?.code && (
+ ``
+ )
+ ) || ''
+
+ // Create readable stream with prepended and appended chunks
+ const readable = Readable.from(generateHtmlStream({
+ body: app.html,
+ head: headTemplate({
+ ...context,
+ style,
+ head,
+ hydration,
+ }),
+ footer: footerTemplate(context),
+ }))
+ // Send out header and readable stream with full response
+ this.type('text/html')
+ this.send(readable)
+ }
+}
+
+export async function createRenderFunction ({ routes, Root }) {
+ // create is exported by client/index.js
+ return function (req) {
+ // Create convenience-access routeMap
+ const routeMap = Object.fromEntries(routes.toJSON().map((route) => {
+ return [route.path, route]
+ }))
+ // Creates main React component with all the SSR context it needs
+ const app = !req.route.clientOnly && Root.render({
+ url: req.url,
+ payload: {
+ routes,
+ routeMap,
+ serverRoute: req.route,
+ },
+ })
+ // Perform SSR, i.e., turn app.instance into an HTML fragment
+ // The SSR context data is passed along so it can be inlined for hydration
+ return { routes, context: req.route, app }
+ }
+}
+
+export function createRouteHandler (client, scope, config) {
+ return function (req, reply) {
+ reply.html(reply.render(req))
+ return reply
+ }
+}
+
+export function createRoute ({ client, handler, errorHandler, route }, scope, config) {
+ const onRequest = async function onRequest (req, reply) {
+ req.route = await RouteContext.create(
+ scope,
+ req,
+ reply,
+ route,
+ client.context,
+ )
+ }
+ if (route.getData) {
+ // If getData is provided, register JSON endpoint for it
+ scope.get(`/-/data${route.path}`, {
+ onRequest,
+ async handler (req, reply) {
+ reply.send(await route.getData(req.route))
+ },
+ })
+ }
+
+ // See https://github.com/fastify/fastify-dx/blob/main/URMA.md
+ const hasURMAHooks = Boolean(
+ route.getData || route.getMeta || route.onEnter,
+ )
+
+ // Extend with route context initialization module
+ RouteContext.extend(client.context)
+
+ scope.get(route.path, {
+ onRequest,
+ // If either getData or onEnter are provided,
+ // make sure they run before the SSR route handler
+ ...hasURMAHooks && {
+ async preHandler (req, reply) {
+ try {
+ if (route.getData) {
+ req.route.data = await route.getData(req.route)
+ }
+ if (route.getMeta) {
+ req.route.head = await route.getMeta(req.route)
+ }
+ if (route.onEnter) {
+ if (!req.route.data) {
+ req.route.data = {}
+ }
+ const result = await route.onEnter(req.route)
+ Object.assign(req.route.data, result)
+ }
+ } catch (err) {
+ if (config.dev) {
+ console.error(err)
+ }
+ req.route.error = err
+ }
+ },
+ },
+ handler,
+ errorHandler,
+ ...route,
+ })
+}
diff --git a/packages/fastify-dx-svelte/package.json b/packages/fastify-dx-svelte/package.json
index 4af29c5..d31f954 100644
--- a/packages/fastify-dx-svelte/package.json
+++ b/packages/fastify-dx-svelte/package.json
@@ -1,6 +1,44 @@
{
+ "scripts": {
+ "lint": "eslint . --ext .js,.svelte --fix"
+ },
+ "type": "module",
+ "main": "index.js",
"name": "fastify-dx-svelte",
- "version": "0.0.0",
- "files": ["index.js"],
- "license": "MIT"
-}
\ No newline at end of file
+ "version": "0.0.1",
+ "files": [
+ "virtual/root.svelte",
+ "virtual/route.svelte",
+ "virtual/layouts.js",
+ "virtual/layouts/default.svelte",
+ "virtual/context.js",
+ "virtual/mount.js",
+ "virtual/core.js",
+ "virtual/routes.js",
+ "index.js",
+ "plugin.cjs",
+ "server/context.js",
+ "server/stream.js"
+ ],
+ "license": "MIT",
+ "exports": {
+ ".": "./index.js",
+ "./plugin": "./plugin.cjs"
+ },
+ "dependencies": {
+ "devalue": "^2.0.1",
+ "svelte-loadable": "^2.0.1",
+ "svelte-routing": "^1.6.0",
+ "sveltio": "^1.0.5",
+ "unihead": "^0.0.6"
+ },
+ "devDependencies": {
+ "@babel/eslint-parser": "^7.18.2",
+ "eslint": "^8.18.0",
+ "eslint-config-standard": "^16.0.2",
+ "eslint-plugin-import": "^2.22.1",
+ "eslint-plugin-node": "^11.1.0",
+ "eslint-plugin-promise": "^4.3.1",
+ "eslint-plugin-svelte3": "^4.0.0"
+ }
+}
diff --git a/packages/fastify-dx-svelte/plugin.cjs b/packages/fastify-dx-svelte/plugin.cjs
new file mode 100644
index 0000000..ecce70c
--- /dev/null
+++ b/packages/fastify-dx-svelte/plugin.cjs
@@ -0,0 +1,106 @@
+const { readFileSync, existsSync } = require('fs')
+const { dirname, join, resolve } = require('path')
+const { fileURLToPath } = require('url')
+
+function viteSvelteFastifyDX (config = {}) {
+ const prefix = /^\/?dx:/
+ const routing = Object.assign({
+ globPattern: '/pages/**/*.svelte',
+ paramPattern: /\[(\w+)\]/,
+ }, config)
+ const virtualRoot = resolve(__dirname, 'virtual')
+ const virtualModules = [
+ 'mount.js',
+ 'routes.js',
+ 'layouts.js',
+ 'root.svelte',
+ 'route.svelte',
+ 'layouts/',
+ 'context.js',
+ 'core.js'
+ ]
+ virtualModules.includes = function (virtual) {
+ if (!virtual) {
+ return false
+ }
+ for (const entry of this) {
+ if (virtual.startsWith(entry)) {
+ return true
+ }
+ }
+ return false
+ }
+ const virtualModuleInserts = {
+ 'routes.js': {
+ $globPattern: routing.globPattern,
+ $paramPattern: routing.paramPattern,
+ }
+ }
+
+ let viteProjectRoot
+
+ function loadVirtualModuleOverride (virtual) {
+ if (!virtualModules.includes(virtual)) {
+ return
+ }
+ const overridePath = resolve(viteProjectRoot, virtual)
+ if (existsSync(overridePath)) {
+ return overridePath
+ }
+ }
+
+ function loadVirtualModule (virtual) {
+ if (!virtualModules.includes(virtual)) {
+ return
+ }
+ let code = readFileSync(resolve(virtualRoot, virtual), 'utf8')
+ if (virtualModuleInserts[virtual]) {
+ for (const [key, value] of Object.entries(virtualModuleInserts[virtual])) {
+ code = code.replace(new RegExp(escapeRegExp(key), 'g'), value)
+ }
+ }
+ return {
+ code,
+ map: null,
+ }
+ }
+
+ // Thanks to https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js
+ function escapeRegExp (s) {
+ return s
+ .replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
+ .replace(/-/g, '\\x2d')
+ }
+
+ return {
+ name: 'vite-plugin-react-fastify-dx',
+ config (config, { command }) {
+ if (command === 'build' && config.build?.ssr) {
+ config.build.rollupOptions = {
+ output: {
+ format: 'es',
+ },
+ }
+ }
+ },
+ configResolved (config) {
+ viteProjectRoot = config.root
+ },
+ async resolveId (id) {
+ const [, virtual] = id.split(prefix)
+ if (virtual) {
+ const override = await loadVirtualModuleOverride(virtual)
+ if (override) {
+ return override
+ }
+ return id
+ }
+ },
+ load (id) {
+ const [, virtual] = id.split(prefix)
+ return loadVirtualModule(virtual)
+ },
+ }
+}
+
+module.exports = viteSvelteFastifyDX
diff --git a/packages/fastify-dx-svelte/server/context.js b/packages/fastify-dx-svelte/server/context.js
new file mode 100644
index 0000000..fab9a2e
--- /dev/null
+++ b/packages/fastify-dx-svelte/server/context.js
@@ -0,0 +1,65 @@
+
+const routeContextInspect = Symbol.for('nodejs.util.inspect.custom')
+
+export default class RouteContext {
+ static async create (server, req, reply, route, contextInit) {
+ const routeContext = new RouteContext(server, req, reply, route)
+ if (contextInit) {
+ if (contextInit.state) {
+ routeContext.state = contextInit.state()
+ }
+ if (contextInit.default) {
+ await contextInit.default(routeContext)
+ }
+ }
+ return routeContext
+ }
+
+ constructor (server, req, reply, route) {
+ this.server = server
+ this.req = req
+ this.reply = reply
+ this.head = {}
+ this.state = null
+ this.data = route.data
+ this.firstRender = true
+ this.layout = route.layout
+ this.getMeta = !!route.getMeta
+ this.getData = !!route.getData
+ this.onEnter = !!route.onEnter
+ this.streaming = route.streaming
+ this.clientOnly = route.clientOnly
+ this.serverOnly = route.serverOnly
+ }
+
+ [routeContextInspect] () {
+ return {
+ ...this,
+ server: { [routeContextInspect]: () => '[Server]' },
+ req: { [routeContextInspect]: () => '[Request]' },
+ reply: { [routeContextInspect]: () => '[Reply]' },
+ }
+ }
+
+ toJSON () {
+ return {
+ state: this.state,
+ data: this.data,
+ layout: this.layout,
+ getMeta: this.getMeta,
+ getData: this.getData,
+ onEnter: this.onEnter,
+ firstRender: this.firstRender,
+ clientOnly: this.clientOnly,
+ }
+ }
+}
+
+RouteContext.extend = function (initial) {
+ const { default: _, ...extra } = initial
+ for (const [prop, value] of Object.entries(extra)) {
+ if (prop !== 'data' && prop !== 'state') {
+ Object.defineProperty(RouteContext.prototype, prop, value)
+ }
+ }
+}
diff --git a/packages/fastify-dx-svelte/server/stream.js b/packages/fastify-dx-svelte/server/stream.js
new file mode 100644
index 0000000..8b97b8d
--- /dev/null
+++ b/packages/fastify-dx-svelte/server/stream.js
@@ -0,0 +1,7 @@
+
+// Helper function to prepend and append chunks the body stream
+export async function * generateHtmlStream ({ head, body, footer }) {
+ yield head
+ yield body
+ yield footer
+}
diff --git a/packages/fastify-dx-svelte/virtual/context.js b/packages/fastify-dx-svelte/virtual/context.js
new file mode 100644
index 0000000..1e605f5
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/context.js
@@ -0,0 +1,4 @@
+// This file serves as a placeholder
+// if no context.js file is provided
+
+export default () => {}
diff --git a/packages/fastify-dx-svelte/virtual/core.js b/packages/fastify-dx-svelte/virtual/core.js
new file mode 100644
index 0000000..9c267d7
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/core.js
@@ -0,0 +1,28 @@
+import { getContext } from 'svelte'
+import { useSnapshot } from 'sveltio'
+
+export const routeContext = Symbol('routeContext')
+
+export function useRouteContext () {
+ const ctx = getContext(routeContext).routeContext
+ ctx.snapshot = useSnapshot(ctx.state)
+ return ctx
+}
+
+export async function jsonDataFetch (path) {
+ const response = await fetch(`/-/data${path}`)
+ let data
+ let error
+ try {
+ data = await response.json()
+ } catch (err) {
+ error = err
+ }
+ if (data?.statusCode === 500) {
+ throw new Error(data.message)
+ }
+ if (error) {
+ throw error
+ }
+ return data
+}
diff --git a/packages/fastify-dx-svelte/virtual/layouts.js b/packages/fastify-dx-svelte/virtual/layouts.js
new file mode 100644
index 0000000..e188cc1
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/layouts.js
@@ -0,0 +1,12 @@
+import DefaultLayout from '/dx:layouts/default.svelte'
+
+const appLayouts = import.meta.globEager('/layouts/*.svelte')
+
+appLayouts['/layouts/default.svelte'] ??= DefaultLayout
+
+export default Object.fromEntries(
+ Object.keys(appLayouts).map((path) => {
+ const name = path.slice(9, -7)
+ return [name, appLayouts[path]]
+ }),
+)
diff --git a/packages/fastify-dx-svelte/virtual/layouts/default.svelte b/packages/fastify-dx-svelte/virtual/layouts/default.svelte
new file mode 100644
index 0000000..c63d36b
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/layouts/default.svelte
@@ -0,0 +1,8 @@
+
+
+
+
+
diff --git a/packages/fastify-dx-svelte/virtual/mount.js b/packages/fastify-dx-svelte/virtual/mount.js
new file mode 100644
index 0000000..0d8f604
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/mount.js
@@ -0,0 +1,45 @@
+import Head from 'unihead/client'
+import Root from '/dx:root.svelte'
+import routesPromise from '/dx:routes.js'
+
+mount('main')
+
+async function mount (target) {
+ if (typeof target === 'string') {
+ target = document.querySelector(target)
+ }
+ const context = await import('/dx:context.js')
+ const serverRoute = await extendContext(window.route, context)
+ const head = new Head(window.route.head, window.document)
+ const resolvedRoutes = await routesPromise
+ const routeMap = Object.fromEntries(
+ resolvedRoutes.map((route) => [route.path, route]),
+ )
+ new Root({
+ target: document.querySelector('main'),
+ props: {
+ payload: {
+ head,
+ serverRoute,
+ routes: window.routes,
+ routeMap,
+ },
+ },
+ hydrate: !serverRoute.clientOnly,
+ })
+}
+
+async function extendContext (ctx, {
+ // The route context initialization function
+ default: setter,
+ // We destructure state here just to discard it from extra
+ state,
+ // Other named exports from context.js
+ ...extra
+}) {
+ Object.assign(ctx, extra)
+ if (setter) {
+ await setter(ctx)
+ }
+ return ctx
+}
diff --git a/packages/fastify-dx-svelte/virtual/root.svelte b/packages/fastify-dx-svelte/virtual/root.svelte
new file mode 100644
index 0000000..126d02b
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/root.svelte
@@ -0,0 +1,22 @@
+
+
+
+ {#each payload.routes as { path, component }}
+
+
+
+ {/each}
+
diff --git a/packages/fastify-dx-svelte/virtual/route.svelte b/packages/fastify-dx-svelte/virtual/route.svelte
new file mode 100644
index 0000000..978bbd2
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/route.svelte
@@ -0,0 +1,83 @@
+
+
+{#if isServer}
+
+
+
+{:else}
+{#await setupClientRouteContext}{:then}
+
+
+
+{/await}
+{/if}
diff --git a/packages/fastify-dx-svelte/virtual/routes.js b/packages/fastify-dx-svelte/virtual/routes.js
new file mode 100644
index 0000000..617eec2
--- /dev/null
+++ b/packages/fastify-dx-svelte/virtual/routes.js
@@ -0,0 +1,119 @@
+/* global $paramPattern */
+
+export default import.meta.env.SSR
+ ? createRoutes(import.meta.globEager('$globPattern'))
+ : hydrateRoutes(import.meta.glob('$globPattern'))
+
+async function createRoutes (from, { param } = { param: $paramPattern }) {
+ // Otherwise we get a ReferenceError, but since
+ // this function is only ran once, there's no overhead
+ class Routes extends Array {
+ toJSON () {
+ return this.map((route) => {
+ return {
+ id: route.id,
+ path: route.path,
+ layout: route.layout,
+ getData: !!route.getData,
+ getMeta: !!route.getMeta,
+ onEnter: !!route.onEnter,
+ }
+ })
+ }
+ }
+ const importPaths = Object.keys(from)
+ const promises = []
+ if (Array.isArray(from)) {
+ for (const routeDef of from) {
+ promises.push(
+ getRouteModule(routeDef.path, routeDef.component)
+ .then((routeModule) => {
+ return {
+ id: routeDef.path,
+ path: routeDef.path ?? routeModule.path,
+ ...routeModule,
+ }
+ }),
+ )
+ }
+ } else {
+ // Ensure that static routes have precedence over the dynamic ones
+ for (const path of importPaths.sort((a, b) => a > b ? -1 : 1)) {
+ promises.push(
+ getRouteModule(path, from[path])
+ .then((routeModule) => {
+ return {
+ id: path,
+ layout: routeModule.layout,
+ path: routeModule.path ?? path
+ // Remove /pages and .jsx extension
+ .slice(6, -7)
+ // Replace [id] with :id
+ .replace(param, (_, m) => `:${m}`)
+ // Replace '/index' with '/'
+ .replace(/\/index$/, '/')
+ // Remove trailing slashs
+ .replace(/.+\/+$/, ''),
+ ...routeModule,
+ }
+ }),
+ )
+ }
+ }
+ return new Routes(...await Promise.all(promises))
+}
+
+async function hydrateRoutes (from) {
+ if (Array.isArray(from)) {
+ from = Object.fromEntries(
+ from.map((route) => [route.path, route]),
+ )
+ }
+ return window.routes.map((route) => {
+ route.loader = memoImport(from[route.id])
+ route.component = () => route.loader()
+ return route
+ })
+}
+
+function getRouteModuleExports (routeModule) {
+ return {
+ // The Route component (default export)
+ component: routeModule.default,
+ // The Layout Route component
+ layout: routeModule.layout,
+ // Route-level hooks
+ getData: routeModule.getData,
+ getMeta: routeModule.getMeta,
+ onEnter: routeModule.onEnter,
+ // Other Route-level settings
+ streaming: routeModule.streaming,
+ clientOnly: routeModule.clientOnly,
+ serverOnly: routeModule.serverOnly,
+ }
+}
+
+async function getRouteModule (path, routeModule) {
+ // const isServer = typeof process !== 'undefined'
+ if (typeof routeModule === 'function') {
+ routeModule = await routeModule()
+ return getRouteModuleExports(routeModule)
+ } else {
+ return getRouteModuleExports(routeModule)
+ }
+}
+
+function memoImport (func) {
+ // Otherwise we get a ReferenceError, but since this function
+ // is only ran once for each route, there's no overhead
+ const kFuncExecuted = Symbol('kFuncExecuted')
+ const kFuncValue = Symbol('kFuncValue')
+ func[kFuncExecuted] = false
+ return async function () {
+ if (!func[kFuncExecuted]) {
+ func[kFuncValue] = await func()
+ func[kFuncExecuted] = true
+ }
+ return func[kFuncValue]
+ }
+}
diff --git a/starters/react/client/pages/client-only.jsx b/starters/react/client/pages/client-only.jsx
index b55bf1d..2e72f69 100644
--- a/starters/react/client/pages/client-only.jsx
+++ b/starters/react/client/pages/client-only.jsx
@@ -3,21 +3,21 @@ import { Link } from 'react-router-dom'
export const clientOnly = true
export function getMeta () {
- return {
- title: 'Client Only Page'
- }
+ return {
+ title: 'Client Only Page'
+ }
}
export default function ClientOnly () {
- return (
- <>
-
This route is rendered on the client only!
+ return (
+ <>
+
This route is rendered on the client only!
Go back to the index
⁂
When this route is rendered on the server, no SSR takes place.
See the output of curl http://localhost:3000/client-only.
/using-data demonstrates how to
+ leverage the getData() function
+ and useRouteContext() to retrieve server data for a route.
+
/using-store demonstrates how to
+ leverage the
+ automated Valtio store
+ to retrieve server data for a route and maintain it in a global
+ state even after navigating to another route.
+
/using-auth demonstrates how to
+ wrap a route in a custom layout component.
+
/client-only demonstrates how to set
+ up a route for rendering on the client only (disables SSR).
+
/server-only demonstrates how to set
+ up a route for rendering on the server only (sends no JavaScript).