routify icon indicating copy to clipboard operation
routify copied to clipboard

Routify gives "Error: Function called outside component initialization" when used with Vite, only in Dev Build Mode

Open derpycoder opened this issue 5 years ago • 16 comments

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.

` Error: Function called outside component initialization at get_current_component (http://localhost:3000/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:649:15) at setContext (http://localhost:3000/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:679:5) at instance (http://localhost:3000/node_modules/.pnpm/@roxi/[email protected]/node_modules/@roxi/routify/runtime/Router.svelte?import:176:2) at init (http://localhost:3000/home/abhijit/Documents/Experiments/QuillerBee/Sub%20Experiments/vitte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:1474:11) at new Router (http://localhost:3000/node_modules/.pnpm/@roxi/[email protected]/node_modules/@roxi/routify/runtime/Router.svelte?import:264:3) at createProxiedComponent (http://localhost:3000/node_modules/.pnpm/[email protected][email protected]/node_modules/svelte-hmr/runtime/svelte-hooks.js:245:9) at new ProxyComponent (http://localhost:3000/node_modules/.pnpm/[email protected][email protected]/node_modules/svelte-hmr/runtime/proxy.js:240:20) at new Proxy (http://localhost:3000/node_modules/.pnpm/[email protected][email protected]/node_modules/svelte-hmr/runtime/proxy.js:340:11) at create_fragment (http://localhost:3000/src/App.svelte?import:26:11) at init (http://localhost:3000/home/abhijit/Documents/Experiments/QuillerBee/Sub%20Experiments/vitte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:1489:37)

Error: Function called outside component initialization at get_current_component (http://localhost:3000/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:649:15) at setContext (http://localhost:3000/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:679:5) at instance (http://localhost:3000/node_modules/.pnpm/@roxi/[email protected]/node_modules/@roxi/routify/runtime/Router.svelte?import:176:2) at init (http://localhost:3000/home/abhijit/Documents/Experiments/QuillerBee/Sub%20Experiments/vitte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:1474:11) at new Router (http://localhost:3000/node_modules/.pnpm/@roxi/[email protected]/node_modules/@roxi/routify/runtime/Router.svelte?import:264:3) at createProxiedComponent (http://localhost:3000/node_modules/.pnpm/[email protected][email protected]/node_modules/svelte-hmr/runtime/svelte-hooks.js:245:9) at new ProxyComponent (http://localhost:3000/node_modules/.pnpm/[email protected][email protected]/node_modules/svelte-hmr/runtime/proxy.js:240:20) at new Proxy (http://localhost:3000/node_modules/.pnpm/[email protected][email protected]/node_modules/svelte-hmr/runtime/proxy.js:340:11) at create_fragment (http://localhost:3000/src/App.svelte?import:26:11) at init (http://localhost:3000/home/abhijit/Documents/Experiments/QuillerBee/Sub%20Experiments/vitte/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.mjs?v=f03f2977:1489:37) `

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 no-work

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. 🙃

derpycoder avatar Mar 23 '21 06:03 derpycoder

Hey the issue was because of a space in parent folder name.

Svelte Kit Issue Resolution

derpycoder avatar Mar 24 '21 17:03 derpycoder

Happy you got it fixed @abhijit-kar !

jakobrosenberg avatar Mar 24 '21 17:03 jakobrosenberg

@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.

derpycoder avatar Mar 25 '21 05:03 derpycoder

You can try this repo https://github.com/roxiness/routify-templates

It should be able to give you a working vite template.

jakobrosenberg avatar Mar 25 '21 06:03 jakobrosenberg

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.

derpycoder avatar Mar 25 '21 07:03 derpycoder

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

jakobrosenberg avatar Mar 25 '21 07:03 jakobrosenberg

Damn, I am going to copy so many configs from that blueprint.

Thanks.

derpycoder avatar Mar 25 '21 07:03 derpycoder

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!

macleod-ee avatar May 17 '21 06:05 macleod-ee

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.

braebo avatar May 18 '21 00:05 braebo

@drannex42 @FractalHQ what would be the best place for Vite documentation? FAQ or a dedicated section?

jakobrosenberg avatar May 18 '21 07:05 jakobrosenberg

My vote would be dedicated. I imagine it will be the de facto bundler for most users.

braebo avatar May 18 '21 14:05 braebo

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.

macleod-ee avatar May 18 '21 18:05 macleod-ee

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)

agrear avatar Jun 13 '21 20:06 agrear

what version of vite-plugin-svelte @agrear

ghostdevv avatar Jun 13 '21 21:06 ghostdevv

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.

agrear avatar Jun 13 '21 21:06 agrear

Replacing import { BaseTransition } from '@roxi/routify/decorators' with import BaseTransition from '@roxi/routify/runtime/decorators/BaseTransition.svelte' fixes the issue.

agrear avatar Jun 16 '21 13:06 agrear