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

Next 13: experimental appDir breaks useSWR #42249

Closed
1 task done
andrewcoelho opened this issue Oct 31, 2022 · 9 comments · Fixed by #42741
Closed
1 task done

Next 13: experimental appDir breaks useSWR #42249

andrewcoelho opened this issue Oct 31, 2022 · 9 comments · Fixed by #42741
Assignees
Labels
area: app App directory (appDir: true)

Comments

@andrewcoelho
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000
Binaries:
Node: 18.12.0
npm: 8.19.2
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 13.0.1-canary.2
eslint-config-next: 13.0.0
react: 18.2.0
react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Default Home component from yarn create next-app

export default function Home() {
  const { data, error } = useSWR("/api/hello", (...args) =>
    fetch(...args).then((res) => res.json())
  );
  ...
}

errors with
TypeError: Cannot read properties of null (reading 'useContext') with an invalid hook call warning

removing

experimental: {
  appDir: true,
},

from next.config.js resolves the issue.

Expected Behavior

useSWR should work when the experimental app directory is enabled

Link to reproduction

https://stackblitz.com/edit/vercel-next-js-arthzt?file=pages/index.tsx

To Reproduce

use

experimental: {
  appDir: true
}

and useSWR from swr together

@andrewcoelho andrewcoelho added the bug Issue was opened via the bug report template. label Oct 31, 2022
@balazsorban44 balazsorban44 added area: app App directory (appDir: true) kind: bug and removed bug Issue was opened via the bug report template. labels Oct 31, 2022
@armandodlvr
Copy link

I think the problem is that you are using SWRConfig in a server component. Remove it and useSWR will work fine.

@shuding
Copy link
Member

shuding commented Nov 1, 2022

useSWR is client only and cannot be used in server components. You can add "use client" on the top of your page file to convert it to a client component.

@shuding shuding closed this as completed Nov 1, 2022
@andrewcoelho
Copy link
Author

@shuding that doesn't seem like a solution considering this problem is happening in the /pages directory and the behavior in that directory is supposed to be unchanged to allow for incremental adoption.

If you look at the reproduction link I posted you can see that there isn't an /app directory being used at all. Simply adding the experimental appDir flag causes this issue.

@shuding shuding reopened this Nov 1, 2022
@shuding
Copy link
Member

shuding commented Nov 1, 2022

Thought it was in app/ — yeah that's indeed a bug if it happens in pages/!

@lukemorales
Copy link

lukemorales commented Nov 2, 2022

It seems that the repro is missing the app directory, perhaps it's breaking because by enabling the feature, Next makes app the root directory and if you don't have the root layout.tsx it breaks because it doesn't have an HTML to render? Just fixed another issue reproduction by doing exactly that: #42263 (comment)

@shuding
Copy link
Member

shuding commented Nov 2, 2022

Thanks for your input @lukemorales and that's indeed the case! Although Next.js shouldn't behavior like that, at least shouldn't throw such a strange error. Let me see if I can fix it or improve the error.

@armandodlvr
Copy link

It was fixed in version 13.0.2

@rafinskipg
Copy link

happened to me too on version 13.0.2, removed the app dir experimental feature and worked again

shuding added a commit that referenced this issue Nov 10, 2022
One potential risk is ESM dependencies that can't be bundled will cause
a build error. This also means that the `esmExternals` configuration
will be affected.

Closes #42249, closes #42588. Related: #1395.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants