• Stars
    star
    182
  • Rank 211,154 (Top 5 %)
  • Language
    Rust
  • Created over 2 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SuperJSON Plugin for Next.js Pages and Components

Next SuperJSON Plugin

πŸ”Œ SuperJSON Plugin for Next.js (SWC)

/pages (Pages Directory)

export default function Page({ date }) {
  return <div>Today is {date.toDateString()}</div>;
}

// You can also use getInitialProps, getStaticProps
export const getServerSideProps = () => {
  return {
    props: {
      date: new Date(),
    },
  };
};
  • Allows pre-rendering functions to return props including Non-JSON Values(Date, Map, Set..)

/app (App Directory)

// Use "data-superjson" attribute to pass non-serializable props to client components
// No needs to change the propsType of Client Component (It's type-safe!)

export default function ServerComponent() {
  const date = new Date();
  return <ClientComponent date={date} data-superjson />;
}

Usage

Install packages first:

npm install superjson next-superjson-plugin
# or Yarn
yarn add superjson next-superjson-plugin

Add the plugin into next.config.js

// next.config.js
module.exports = {
  experimental: {
    swcPlugins: [["next-superjson-plugin", {}]],
  },
};

Options

You can use the excluded option to exclude specific properties from serialization.

['next-superjson-plugin', { excluded: ["someProp"] }],

How it works

sequenceDiagram
    participant Next.js
    participant SWC Plugin
    participant SuperJSON
    Next.js->>SWC Plugin: Request Transform
    SWC Plugin->>SWC Plugin: Transform Pages/Components <br> To Use SuperJSON
    SWC Plugin->>Next.js: Take Modules
    Next.js-->SuperJSON: Connected
    Next.js->>SuperJSON: Serialize Props <br> (Date, BigInt, Set, Map..)
    Note over SWC Plugin: getInitialProps <br> getServerSideProps <br> getStaticProps <br> Server Components
    SuperJSON->>Next.js: Deserialize Props
    Note over SWC Plugin: Pages <br> Client Components

Bug Report

⚠️ Keep in mind: SWC Plugin is still an experimental feature for Next.js

Plugin always ensures compatibility with Next.js Canary version only.

Leave an Issue

Special Thanks

  • kdy1 (Main creator of swc project)

More Repositories

1

blitz

⚑️ The Missing Fullstack Toolkit for Next.js
TypeScript
13,675
star
2

superjson

Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
TypeScript
3,746
star
3

blitzjs.com

Website & docs
MDX
180
star
4

babel-plugin-superjson-next

Automatically transform your Next.js Pages to use SuperJSON
TypeScript
125
star
5

awesome-blitzjs

Curated list of awesome resources for blitz.js
90
star
6

blitzjs.com-translation

Nexus of resources and tools for translating the Blitz docs.
TypeScript
14
star
7

ja.blitzjs.com

(Work in progress) Blitzjs.com website in Japanese!
JavaScript
13
star
8

github-bot

Our very own Github Bot
TypeScript
6
star
9

codesandbox-template

TypeScript
5
star
10

drafts

A place for RFCs and other documents to be drafted before official publication
5
star
11

zh-hans.blitzjs.com

(Work in progress) Blitzjs.com website in Simplified Chinese!
JavaScript
4
star
12

fr.blitzjs.com

(Work in progress) Blitzjs.com website in French!
JavaScript
4
star
13

blitz-realworld-example

TypeScript
4
star
14

art

Blitz.js Branding Assets
3
star
15

es.blitzjs.com

(Work in progress) Blitzjs.com website in Spanish!
JavaScript
3
star
16

discord-bot

The bot used on the Blitz Community discord server
TypeScript
3
star
17

id.blitzjs.com

(Work in progress) Blitzjs.com website in Indonesian!
JavaScript
2
star
18

community-intros-matcher

Algorithm to generate community intros matches.
TypeScript
2
star
19

legacy-framework

JavaScript
2
star
20

versions-diff-web

A web tool to support Blitz developers in upgrading their apps.
JavaScript
2
star
21

versions-diff

Shell
2
star
22

it.blitzjs.com

(Work in progress) Blitzjs.com website in Italian!
JavaScript
1
star
23

ko.blitzjs.com

(Work in progress) Blitzjs.com website in Korean!
JavaScript
1
star
24

pt-br.blitzjs.com

(Work in progress) Blitzjs.com website in Brazilian Portuguese!
JavaScript
1
star