Skip to content

Commit c921260

Browse files
committed
[docs] add FAQ for Vite middleware
1 parent 23ff8b5 commit c921260

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

documentation/faq/80-integrations.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,34 @@ onMount(() => {
6565

6666
Put the code to query your database in [endpoints](/docs#routing-endpoints) - don't query the database in .svelte files. You can create a `db.js` or similar that sets up a connection immediately and makes the client accessible throughout the app as a singleton. You can execute any one-time setup code in `hooks.js` and import your database helpers into any endpoint that needs them.
6767

68+
### How use middleware?
69+
70+
In dev, you can add middleware to Vite by using a Vite plugin. For example:
71+
72+
```js
73+
const myPlugin = {
74+
name: 'log-request-middleware',
75+
configureServer(server) {
76+
server.middlewares.use((req, res, next) => {
77+
console.log(`Got request ${req.url}`);
78+
next();
79+
})
80+
}
81+
}
82+
83+
/** @type {import('@sveltejs/kit').Config} */
84+
const config = {
85+
kit: {
86+
target: '#svelte',
87+
vite: {
88+
plugins: [ myPlugin ]
89+
}
90+
}
91+
};
92+
93+
export default config;
94+
```
95+
6896
### Does it work with Yarn 2?
6997

7098
Sort of. The Plug'n'Play feature, aka 'pnp', is broken (it deviates from the Node module resolution algorithm, and [doesn't yet work with native JavaScript modules](https://github.com/yarnpkg/berry/issues/638) which SvelteKit — along with an [increasing number of packages](https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77) — uses). You can use `nodeLinker: 'node-modules'` in your [`.yarnrc.yml`](https://yarnpkg.com/configuration/yarnrc#nodeLinker) file to disable pnp, but it's probably easier to just use npm or [pnpm](https://pnpm.io/), which is similarly fast and efficient but without the compatibility headaches.

0 commit comments

Comments
 (0)