Skip to content
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

[Example] remove getInitialProps from aws-amplify #13896

Merged
merged 10 commits into from Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/with-aws-amplify/README.md
Expand Up @@ -6,8 +6,8 @@ This example shows how to build a server rendered web application with NextJS an

Two routes are implemented :

- `/` : A static route that uses getInitialProps to load data from AppSync and renders it on the server (Code in [pages/index.js](/pages/index.js))
- `/todo/[id]` : A dynamic route that uses getInitialProps and the id from the provided context to load a single todo from AppSync and render it on the server. (Code in [pages/todo/:[id].js](/pages/todo/[id].js))
- `/` : A static route that uses getStaticProps to load data from AppSync and renders it on the server (Code in [pages/index.js](/pages/index.js))
- `/todo/[id]` : A dynamic route that uses getServerSideProps and the id from the provided context to load a single todo from AppSync and render it on the server. (Code in [pages/todo/:[id].js](/pages/todo/[id].js))

## How to use

Expand Down
2 changes: 1 addition & 1 deletion examples/with-aws-amplify/package.json
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"aws-amplify": "1.1.32",
"aws-amplify": "2.1.0",
"immer": "3.1.3",
"nanoid": "2.0.3",
"next": "^9.0.2",
Expand Down
22 changes: 18 additions & 4 deletions examples/with-aws-amplify/pages/index.js
Expand Up @@ -111,16 +111,25 @@ const App = (props) => {
</div>
)
}
App.getInitialProps = async () => {

export const getStaticProps = async () => {
let result = await API.graphql(
graphqlOperation(getTodoList, { id: 'global' })
)
if (result.errors) {
console.log('Failed to fetch todolist. ', result.errors)
return { todos: [] }
return {
props: {
todos: [],
},
}
}
if (result.data.getTodoList !== null) {
return { todos: result.data.getTodoList.todos.items }
return {
props: {
todos: result.data.getTodoList.todos.items,
},
}
}

try {
Expand All @@ -135,6 +144,11 @@ App.getInitialProps = async () => {
} catch (err) {
console.warn(err)
}
return { todos: [] }
return {
props: {
todos: [],
},
}
}

export default App
20 changes: 16 additions & 4 deletions examples/with-aws-amplify/pages/todo/[id].js
Expand Up @@ -14,7 +14,7 @@ const TodoPage = (props) => {
)
}

TodoPage.getInitialProps = async (context) => {
export const getServerSideProps = async (context) => {
const { id } = context.query
try {
const todo = await API.graphql({
Expand All @@ -23,12 +23,24 @@ TodoPage.getInitialProps = async (context) => {
})
if (todo.errors) {
console.log('Failed to fetch todo. ', todo.errors)
return { todo: {} }
return {
props: {
todo: {},
},
}
}
return {
props: {
todo: todo.data.getTodo,
},
}
return { todo: todo.data.getTodo }
} catch (err) {
console.log('Failed to fetch todo. ', err)
return { todo: {} }
return {
props: {
todo: {},
},
}
}
}

Expand Down