Skip to content

Change README component from class to functional component #30

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 20 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -73,35 +73,27 @@ const Index = () => (
// components/MyFragment.js
import React from 'react';

export default class MyFragment extends React.Component {
render() {
return (
<section>
<h1>A fragment that can have its own TTL</h1>

<div>{this.props.greeting /* access to the props as usual */}</div>
<div>{this.props.dataFromAnAPI}</div>
</section>
);
}
export default function MyFragment(props) {
return (
<section>
<h1>A fragment that can have its own TTL</h1>

<div>{props.greeting /* access to the props as usual */}</div>
<div>{props.dataFromAnAPI}</div>
</section>
)
}

export async function getStaticProps() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should be reverted. It's mandatory to set the Cache-Control header for the cache server to store this fragment in cache.

However we should use getServerSideProps() instead of getServerInitialProps(), which is now recommended.

const res = await fetch(/* any api */)
const data = await res.json()

static async getInitialProps({ props, req, res }) {
return new Promise(resolve => {
if (res) {
// Set a TTL for this fragment
res.set('Cache-Control', 's-maxage=60, max-age=30');
}

// Simulate a delay (call to a remote service such as a web API)
setTimeout(
() =>
resolve({
...props, // Props coming from index.js, passed through the internal URL
dataFromAnAPI: 'Hello there'
}),
2000
);
});
return {
props: {
greeting: 'Hello there',
dataFromAnAPI: data
},
revalidate: 60,
}
}
```