Routify gives "Error: Function called outside component initialization" when used with Vite, only in Dev Build Mode
bug description I love Routify, and am able to use it with Rollup without issues. See: Quiller Bee & Quiller Bee Repo.
However, since Rollup is slow, I decided to use Vite.
After integrating Routify, the production build works great, but the Dev build gives error. See Vitte Repo: vite-routify-issue branch. And I really need the speed of dev build with Vite & especially the HMR support.
`
package.json
"scripts": {
"dev": "vite",
"routify": "routify",
"dev:routify": "run-p dev routify",
"fix": "pnpx eslint --fix \"{,!(node_modules|public|dist|.routify)/**/}*.{js,svelte}\"",
"format": "pnpx prettier --write --plugin-search-dir=. \"{,!(node_modules|public|dist|.routify)/**/}*.{js,svelte,json}\"",
"prebuild": "test \"$CI\" = true && npx pnpm install --store=node_modules/.pnpm-store || echo skiping pnpm install",
"build": "routify -b && vite build",
"serve": "vite preview"
},
version
"@roxi/routify": "^2.15.1",
"svelte": "^3.35.0",
"vite": "^2.1.0",
screenshot

P.S. I am going to write a blog post about Vite + Routify combo in my blog DerpyCoder and also use it to replace my Rollup based Dev env. So I need this to work. 🙃
Happy you got it fixed @abhijit-kar !
@jakobrosenberg,
After the success of my repo, where I experimented with Vite + Routify.
I integrated vite with my project. It gave the same error, even though there is no space in any enclosing folder.
See: Quiller Bee - vitte branch
npm run routify
npm run dev
To recreate the issue.
You can try this repo https://github.com/roxiness/routify-templates
It should be able to give you a working vite template.
Hey @jakobrosenberg,
I initialized the starter repo directly from a working Svelte template. However adding routify, urql, & having space in folder caused the issue.
So after going through several reddit threads, git issues, I deduced that:
- Removing space in enclosing folder of the project &
- Adding this to the
vite.config.js.
optimizeDeps: {
exclude: ["@roxi/routify", "@urql/svelte"]
},
Solves the issue.
Ah yes. You can see how we build the vite template here:
https://github.com/roxiness/routify-templates/blob/5b1e3bdcb347a273a7e596cc5c42afd0c0661b1b/fragments/svite/blueprint.js#L17-L37
Damn, I am going to copy so many configs from that blueprint.
Thanks.
I feel like 'optimizeDeps: { exclude: ["@roxi/routify", "@urql/svelte"] },' needs to be included somewhere in the docs for users of Vite, because this was driving me absolutely mad.
Thanks for suggesting it @abhijit-kar!
I feel like 'optimizeDeps: { exclude: ["@roxi/routify", "@urql/svelte"] },' needs to be included somewhere in the docs for users of Vite, because this was driving me absolutely mad.
Yea, at least the top of the urql-svelte docs mention it now.
Unfortunately, adding "@urql/svelte" to optimizeDeps.exclude didn't fix the issue for me. Do you have urql in dependencies or devDependencies?
Could you possible share a working implementation? Perhaps my client initialization is in the wrong file or something. It seems like this is mostly due to urql client using setContext under the hood.
@drannex42 @FractalHQ what would be the best place for Vite documentation? FAQ or a dedicated section?
My vote would be dedicated. I imagine it will be the de facto bundler for most users.
Agreed with @FractalHQ, should be a dedicated section.
Re: url1 - I just took a look, and while I thought I had urlq as a dep, I don't. I just needed to add @roxi/routify to my exclude.
Here is my vite.config.js if it helps:
import { defineConfig } from 'vite'
import svelte from '@sveltejs/vite-plugin-svelte'
import WindiCSS from 'vite-plugin-windicss'
import viteCompression from 'vite-plugin-compression';
import { getAliases } from 'vite-aliases'
const aliases = getAliases();
// https://vitejs.dev/config/
export default defineConfig({
plugins: [WindiCSS(), svelte(), viteCompression()],
resolve: {
alias: aliases
},
optimizeDeps: { exclude: ["@roxi/routify"] },
})
A lot of that isn't needed, but that's my projects config file.
optimizeDeps: { exclude: ["@roxi/routify", "@urql/svelte"] },
This doesn't seem to work if you're using transitions. It throws the following error:
Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:3000/src/routes/_layout.svelte
Might be a problem with vite-plugin-svelte judging from the output:
[vite] Internal server error: failed to transform tagged svelte request for id /node_modules/@roxi/routify/runtime/decorators/BaseTransition.svelte?svelte&type=style&lang.css
Plugin: vite-plugin-svelte
File: /node_modules/@roxi/routify/runtime/decorators/BaseTransition.svelte?svelte&type=style&lang.css
at TransformContext.transform (\node_modules\@sveltejs\vite-plugin-svelte\dist\index.cjs:888:15)
at Object.transform (\node_modules\vite\dist\node\chunks\dep-bc228bbb.js:44765:53)
at transformRequest (\node_modules\vite\dist\node\chunks\dep-bc228bbb.js:59029:51)
at async viteTransformMiddleware (\node_modules\vite\dist\node\chunks\dep-bc228bbb.js:59167:32)
what version of vite-plugin-svelte @agrear
what version of vite-plugin-svelte @agrear
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.11"
I also tried earlier versions up to 1.0.0-next.8.
Replacing import { BaseTransition } from '@roxi/routify/decorators'
with import BaseTransition from '@roxi/routify/runtime/decorators/BaseTransition.svelte'
fixes the issue.