Skip to content

Commit 632cd8f

Browse files
authored
Update README.md (#22)
1 parent 01746ca commit 632cd8f

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

README.md

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,79 @@
1-
# next-superjson-plugin
1+
# 🔌 NEXT SUPERJSON PLUGIN
22

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+
```
321

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>
525

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.**
727

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)**
931

1032
## Usage
1133

1234
Install packages first:
1335

14-
```
36+
```sh
1537
npm install superjson next-superjson-plugin
16-
```
17-
18-
or using Yarn:
19-
20-
```
38+
# or Yarn
2139
yarn add superjson next-superjson-plugin
2240
```
2341

24-
Then modify `next.config.js` in the root directory of your Next.js project:
42+
Add the plugin into `next.config.js`
2543

2644
```js
2745
// next.config.js
2846
module.exports = {
2947
experimental: {
3048
swcPlugins: [
31-
[
32-
'next-superjson-plugin',
33-
{
34-
excluded: [],
35-
},
36-
],
49+
['next-superjson-plugin', {}],
3750
],
3851
},
3952
}
4053
```
4154

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.
4457
```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+
4877
```
4978

5079
## Contributing

0 commit comments

Comments
 (0)