question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature]: Use Blitz.js superjson instead of native JSON.serialize/parse

See original GitHub issue

What is the new or updated feature that you are suggesting?

Use superjson to serialize and parse data returned from loaders/actions.

Why should this feature be included?

Right now Remix uses JSON.stringify and JSON.parse, this means types like Date (see #634) are going to be converted to strings when serializing it but not converted back to Date on the revive step, and other types like BigInt are not even compatible with JSON.stringify, so if an ORM returns one the user needs to manually convert them to a string before returning the data/response inside the loader.

This superjson library created by the Blitz.js team have differents stringify and parse method that it’s capable of serializing more types of data and reviving them back to the original class (see the table here https://github.com/blitz-js/superjson#parse), this way if a loader does this:

return json({ date: new Date() })

Then the useLoaderData will get this

let { date } = useLoaderData<{ date: Date }>()
let fullYear = date.getFullYear() // this will work

And other native objects like BigInt, RegExp, Set, Map and Error will also work as valid return values.


Additionally, superjson can be extended to serialize and revive other objects, it may be cool if Remix let you customize that somehow, but I think just the default config would be better than the native JSON methods.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:21
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sergiodxacommented, Nov 26, 2021

I’m not sure what’s happening with GitHub

@sergiodxa I think that’s because you’re not creating a PR against the default branch (which is main)

Ahhh that could be the reason, I think the team should set the default branch as dev so people open PRs automatically against that branch instead of main

2reactions
mikestopcontinuescommented, Nov 25, 2021

It’s 8.7kB gzipped. I don’t think it’s worth including. It goes against the “web fundamentals” approach of Remix. Better to rally TC39 to update the JSON spec instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sergiodxa - GitHub
This superjson library created by the Blitz.js team have differents stringify and parse method that it's capable of serializing more types of data...
Read more >
SuperJSON - JSON on steroids | simonknott.de
SuperJSON is a high-fidelity replacement to JSON.stringify.It supports data types like Date, RegExp, Map, Set etc., preserves referential ...
Read more >
SuperJSON - JSON on steroids : r/javascript - Reddit
SuperJSON supports Arrays, but what about ArrayBuffer? ... did everything except that, and then add Blitz.js support for their use case?
Read more >
HTTP Middleware - Blitz.js
BlitzServerMiddleware is a utility function that can be used to add a global server middleware. It converts a request middleware to a Blitz-compatible...
Read more >
Remote Code Execution via Prototype Pollution in Blitz.js
We discovered a Prototype Pollution vulnerability (CVE-2022-23631) in the serialization library superjson used in the RPC layer of Blitz.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found