|
1 | | -# next-superjson-plugin |
| 1 | +# 🔌 NEXT SUPERJSON PLUGIN |
2 | 2 |
|
| 3 | +```jsx |
| 4 | +export default Page({ date }) { |
| 5 | + return ( |
| 6 | + <div> |
| 7 | + Today is {date.toDateString()} |
| 8 | + </div> |
| 9 | + ) |
| 10 | +} |
| 11 | + |
| 12 | +// You can also use getInitialProps, getStaticProps |
| 13 | +export const getServerSideProps = () => { |
| 14 | + return { |
| 15 | + props: { |
| 16 | + date: new Date() |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
3 | 21 |
|
4 | | -next-superjson-plugin is a [SWC](https://swc.rs) plugin of `SuperJSON` for `Next.js (>=12.2.0)` |
| 22 | +<p align="middle"> |
| 23 | +<strong> SWC Plugin for Next.js (≥ v12.2.4)</strong> |
| 24 | +</p> |
5 | 25 |
|
6 | | -It supports transforming `getServerSideProps` & `getStaticProps` into [SuperJSON](https://github.com/blitz-js/superjson) functions |
| 26 | +This plugin allows the pre-rendering functions to return props **including complex objects(Date, Map, Set..) that cannot be serialized by standard JSON.** |
7 | 27 |
|
8 | | -So that makes available to use complex objects(**Date, Map, Set..**) in props of pre-rendered pages |
| 28 | +**👀 See [how it works](#how-it-works)** |
| 29 | + |
| 30 | +**Supported object types: [Here](https://github.com/blitz-js/superjson#parse)** |
9 | 31 |
|
10 | 32 | ## Usage |
11 | 33 |
|
12 | 34 | Install packages first: |
13 | 35 |
|
14 | | -``` |
| 36 | +```sh |
15 | 37 | npm install superjson next-superjson-plugin |
16 | | -``` |
17 | | - |
18 | | -or using Yarn: |
19 | | - |
20 | | -``` |
| 38 | +# or Yarn |
21 | 39 | yarn add superjson next-superjson-plugin |
22 | 40 | ``` |
23 | 41 |
|
24 | | -Then modify `next.config.js` in the root directory of your Next.js project: |
| 42 | +Add the plugin into `next.config.js` |
25 | 43 |
|
26 | 44 | ```js |
27 | 45 | // next.config.js |
28 | 46 | module.exports = { |
29 | 47 | experimental: { |
30 | 48 | swcPlugins: [ |
31 | | - [ |
32 | | - 'next-superjson-plugin', |
33 | | - { |
34 | | - excluded: [], |
35 | | - }, |
36 | | - ], |
| 49 | + ['next-superjson-plugin', {}], |
37 | 50 | ], |
38 | 51 | }, |
39 | 52 | } |
40 | 53 | ``` |
41 | 54 |
|
42 | | -### Option |
43 | | -You can use the `exclude` option to exclude specific properties from serialization. |
| 55 | +### Options |
| 56 | +You can use the `excluded` option to exclude specific properties from serialization. |
44 | 57 | ```js |
45 | | -{ |
46 | | - excluded: ["someProp"], |
47 | | -}, |
| 58 | +['next-superjson-plugin', { excluded: ["someProp"] }], |
| 59 | +``` |
| 60 | + |
| 61 | +## How it works |
| 62 | + |
| 63 | +```mermaid |
| 64 | +sequenceDiagram |
| 65 | + participant Next.js |
| 66 | + participant SWC Plugin |
| 67 | + participant SuperJSON |
| 68 | + Next.js->>SWC Plugin: Request Transform |
| 69 | + SWC Plugin->>SWC Plugin: Transform Pages <br> To Use SuperJSON |
| 70 | + SWC Plugin->>Next.js: Take Pages |
| 71 | + Next.js-->SuperJSON: Connected |
| 72 | + Next.js->>SuperJSON: Serialize Props <br> (Date, BigInt, Set, Map..) |
| 73 | + Note over SWC Plugin: getInitialProps <br> getServerSideProps <br> getStaticProps |
| 74 | + SuperJSON->>Next.js: Deserialize Props |
| 75 | + Note over SWC Plugin: Page Component |
| 76 | + |
48 | 77 | ``` |
49 | 78 |
|
50 | 79 | ## Contributing |
|
0 commit comments