Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storybook issue "invariant expected app router to be mounted" #460

Closed
isBatak opened this issue Jan 16, 2024 · 5 comments
Closed

Storybook issue "invariant expected app router to be mounted" #460

isBatak opened this issue Jan 16, 2024 · 5 comments
Labels
next.js Issue is related to Next.js internals rather than this library

Comments

@isBatak
Copy link

isBatak commented Jan 16, 2024

Context

What's your version of nuqs?

"next-usequerystate": "1.15.2"

Next.js information (obtained by running next info):

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:45 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6020
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: 3.2.3
  pnpm: 8.6.12
Relevant Packages:
  next: 14.0.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

Are you using:

  • ❌ The app router
  • ✅ The pages router
  • ❌ The basePath option in your Next.js config
  • ❌ The experimental windowHistorySupport flag in your Next.js config

Description

When running components that uses useQueryState in Storybook this error is shown
image

Storybook version:
"storybook": "7.6.8"
"@storybook/nextjs": "7.6.8"

Docs: https://github.com/storybookjs/storybook/blob/next/code/frameworks/nextjs/README.md#nextjs-navigation

My project uses just pages folder so I don't have appDirectory set to true.

parameters: {
    nextjs: {
      appDirectory: true, // as we are using pages folder I don't have this flag in the story
    },
  },

If I enable it then I get this error
image

This is logical because we are using useRouter in our components.
From next issue https://nextjs.org/docs/messages/next-router-not-mounted

This can also happen when you try to use the useRouter hook from next/router inside the app directory, as the App Router's useRouter from next/navigation has different behavior to the useRouter hook in pages.

Reproduction

CodeSandbox Link

@franky47
Copy link
Member

Thanks for the report. nuqs uses imports from next/navigation to be compatible with both routers under a unified API, but it seems like Storybook doesn't support importing the pages router from there.

This is a gray area of compatibility with Next.js, as the compat layer for the pages router from next/navigation is not feature-complete and downright buggy in some configuration combinations. We had to go around those issues by tapping into Next.js internals in such cases, see #454.

Unfortunately there's not much advice I can give you here but watch that thread in storybookjs/storybook#24722 for potential solutions.

@franky47 franky47 added next.js Issue is related to Next.js internals rather than this library and removed bug Something isn't working labels Jan 16, 2024
@isBatak
Copy link
Author

isBatak commented Jan 17, 2024

Thx @franky47 for the quick response.
I figured this would be the answer from the storybook issue. It seems they are working on the solution for v8 release.
As you can see, I opened issues on bot sides so it's more visible, and helpful to others with the same issue :D
Maybe it's safe to close this issue because there's nothing you can do form the lib perspective.

@franky47 franky47 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 17, 2024
@franky47
Copy link
Member

Sounds good. Feel free to ping back here if/when you find a suitable solution.

@isBatak
Copy link
Author

isBatak commented Jan 24, 2024

Here is the workaround until Storybook fix the issue.

import {
	AppRouterContext,
	type AppRouterInstance,
} from 'next/dist/shared/lib/app-router-context.shared-runtime';

const meta = {
	decorators: [
		(Story) => (
			<AppRouterContext.Provider value={{} as AppRouterInstance}>
				<Story />
			</AppRouterContext.Provider>
		),
	],
};

@isBatak
Copy link
Author

isBatak commented Feb 2, 2024

@franky47 do you maybe know the answer to this question?storybookjs/storybook#24722 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next.js Issue is related to Next.js internals rather than this library
Projects
None yet
Development

No branches or pull requests

2 participants