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

feat: Add temporary dedicated server features imports #805

Closed
wants to merge 8 commits into from

Conversation

franky47
Copy link
Member

@franky47 franky47 commented Dec 12, 2024

This allows using the serializer and the parsers (and later on the loaders) in server-side code without depending on React for the cache, as would be the case if importing from nuqs/server.

🦋 https://bsky.app/profile/francoisbest.com/post/3ld5bqxov722z
X: https://x.com/nuqs47ng/status/1867331081349083400

Tasks

  • Update docs
  • Move everything under a single path named explicitly (nuqs/server/temporary-react-agnostic or something)
  • Rebase and include loaders
  • Investigate import issues with moduleResolution -> try moving the export to nuqs/server-temporary-react-agnostic if it's a nesting issue.

Note

This PR adds three new imports:

  • nuqs/server/cache: exports only the cache feature. Requires React canary (currently only useful in Next.js app router)
  • nuqs/server/parsers: exports defs & all built-in parsers
  • nuqs/server/serializer: exports defs & the createSerializer feature

The nuqs/server import is untouched in this PR.

Future breaking change plans

In nuqs v3:

  • nuqs/server/cache will be the only way to import the cache feature, it will be removed from nuqs/server
  • nuqs/server/parsers and nuqs/server/serializers may be removed for cleanup, as their code will be importable from nuqs/server in a framework-agnostic way.

Closes #804.
See also discussion #717.

Copy link

vercel bot commented Dec 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuqs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 19, 2025 8:35pm

@franky47 franky47 added this to the 🪵 Backlog milestone Dec 12, 2024
Copy link

pkg-pr-new bot commented Dec 12, 2024

pnpm add https://pkg.pr.new/nuqs@805

commit: f7a3217

@franky47 franky47 force-pushed the feat/804-server-imports branch from 6d868dd to 29a5b36 Compare December 14, 2024 09:50
@franky47 franky47 force-pushed the feat/804-server-imports branch from d8489c7 to 729b686 Compare December 27, 2024 10:07
@franky47 franky47 force-pushed the feat/804-server-imports branch from 729b686 to 8d07519 Compare January 1, 2025 10:47
@franky47 franky47 force-pushed the feat/804-server-imports branch from 697736a to d4dcbe3 Compare January 9, 2025 11:11
@franky47 franky47 force-pushed the feat/804-server-imports branch from 1c9229a to aeb4bb6 Compare January 16, 2025 14:20
@franky47 franky47 force-pushed the feat/804-server-imports branch from aeb4bb6 to ad590b8 Compare January 23, 2025 21:02
@franky47 franky47 force-pushed the feat/804-server-imports branch from ad590b8 to 73225a2 Compare January 31, 2025 10:02
@franky47 franky47 force-pushed the feat/804-server-imports branch from 73225a2 to 9a0b899 Compare February 1, 2025 12:43
@hauptrolle
Copy link

Any updates here? I cant import createLoader from nuqs/server with react 18 😭

@franky47
Copy link
Member Author

franky47 commented Feb 6, 2025

@hauptrolle can you try the preview and let me know if it works for you?

pnpm add https://pkg.pr.new/nuqs@805
import { createLoader } from 'nuqs/server/temporary-react-agnostic'

@hauptrolle
Copy link

@franky47 Wow, thank you for the fast response. Looks like the import is working, but typescript is complaining:
Cannot find module "nuqs/server/temporary-react-agnostic" or its corresponding type declarations.

@franky47
Copy link
Member Author

franky47 commented Feb 6, 2025

Do you have a public repo I can take a look at? Or at least, the tsconfig.json, this is the last checkbox in the list for this issue.

@franky47
Copy link
Member Author

franky47 commented Feb 6, 2025

If it's a mater of not being able to nest it under the nuqs/server prefix, maybe we could try nuqs/server-temporary-react-agnostic. 🙃

@hauptrolle
Copy link

The repo is work related so not public sorry ... My tsconfig looks like this:

{
  "include": ["env.d.ts", "**/*.ts", "**/*.tsx"],
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ES2022"],
    "types": ["@remix-run/node", "vite/client"],
    "rootDirs": ["."],
    "isolatedModules": true,
    "esModuleInterop": true,
    "jsx": "react-jsx",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "resolveJsonModule": true,
    "target": "ES2022",
    "strict": true,
    "allowJs": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./app/*"],
      "~e2e/*": ["./e2e-tests/*"],
    },
    "plugins": [
      {
        "name": "typescript-remix-routes-plugin"
      }
    ],

    // Vite takes care of building everything, not tsc.
    "noEmit": true
  }
}

@franky47
Copy link
Member Author

franky47 commented Feb 6, 2025

Ah, if you're using Remix, you might as well import straight from nuqs:

import { createLoader } from 'nuqs'

This PR is mostly to bypass the import of cache from react used in the Next.js app router, when using shared code that doesn't end up in the app router tree (eg: API route handlers, or hybrid app/pages router setups).

Remix and other frameworks don't really care about the 'use client' directive injected at the top of the 'nuqs' import.

Also, you wouldn't need the preview PR version, importing loaders from nuqs is available since 2.3.0.

@hauptrolle
Copy link

Wow, thank you very much. I was following the example here where it looks like I have to import it from nuqs/server. It works now!

Thank you very much for this AWESOME package! <3

@franky47
Copy link
Member Author

franky47 commented Feb 6, 2025

I'll update the docs to note the possible imports, thanks for the kind words!

This allows using the serializer and the parsers in server-side code without
depending on React for the cache, as would be the case if importing
from `nuqs/server`.

Closes #804. See also discussion #717.
For some reason, the classic trick of forwarding everything doesn't work under
`nuqs/server/*`.
We're also removing the temporary imports' `moduleResolution: 'node'` helpers,
as v3 will both remove them and require a moduleResolution set to either 'bundler' or 'nodeNext'.
@franky47
Copy link
Member Author

franky47 commented Feb 19, 2025

@hauptrolle I know the import from nuqs was the immediate fix, but would you be able to try this please? I found another way (a really obvious one that I feel bad for not thinking about sooner) that could potentially solve the initial issue without requiring a dedicated export:

pnpm add https://pkg.pr.new/nuqs@f7a3217e819564e1cd38510f824242b35c32095f

Then in your code, as it was initially:

import { createLoader, createSerializer, parseAsInteger /* etc..*/ } from 'nuqs/server' // Just to skip the "use client" directive

franky47 added a commit that referenced this pull request Feb 21, 2025
Having `import { cache } from 'react'` caused issues when using
React 18 or 19 GA, because the `cache` function is only exported
in canary builds (which the Next.js app router uses internally,
regardless of what's in your app's package.json).

This meant importing from `'nuqs/server'` caused an import error
when done from non app-router code, like the pages router, or
API route definitions, which would fallback to the version of React
defined in the package.json (and likely a stable one).

Changing the import to `import * as React from 'react'` is what is
being highlighted in the React docs themselves, and allows to only
call the cache function when actually creating a cache object.

Closes #804, and supersedes #805.
@franky47
Copy link
Member Author

Closed in favour of #923.

@franky47 franky47 closed this Feb 21, 2025
@franky47 franky47 removed this from the 🪵 Backlog milestone Feb 21, 2025
@franky47 franky47 added the 🪦 graveyard Closed PRs (rejected, wontfix, temporary experiments) label Feb 21, 2025
@franky47 franky47 deleted the feat/804-server-imports branch February 21, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪦 graveyard Closed PRs (rejected, wontfix, temporary experiments)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nuqs/server requires react
2 participants