1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples
Brian Kim 0298c722b1 improve custom-server-fastify example (#4805)
I’ve been experimenting with Next.js and Fastify and I made the following changes to the Fastify example based on what I found:

### Use Fastify’s plugin API
IMO putting Fastify’s listen call in a promise callback is an anti-pattern, b/c the Fastify plugin API is meant to solve the problem of async server bootstrapping.

[From Fastify’s Getting Started docs](https://www.fastify.io/docs/latest/Getting-Started/):
> Fastify provides a foundation that assists with the asynchronous bootstrapping of your application.

### Set reply.sent in handlers which return promises

[From Fastify’s Routes docs](https://www.fastify.io/docs/latest/Routes/#promise-resolution):
> If your handler is an `async` function or returns a promise, you should be aware of a special behaviour which is necessary to support the callback and promise control-flow. If the handler's promise is resolved with `undefined`, it will be ignored causing the request to hang and an *error* log to be emitted.
>
> 1. If you want to use `async/await` or promises but respond a value with `reply.send`:
>     - **Don't** `return` any value.
>     - **Don't** forget to call `reply.send`.
> 2. If you want to use `async/await` or promises:
>     - **Don't** use `reply.send`.
>     - **Don't** return `undefined`.

`app.render` returns a promise which contains undefined, so returning it in a Fastify handler will log an error. However, returning anything besides undefined will cause Fastify to try to write to the response which Next.js has already ended. The solution is to manually set the `reply.sent` flag to true when any Next.js rendering promise is fulfilled as an alternative to calling `reply.send`.

### Make Next.js handle 404 errors
This allows any route to throw a NotFound error and let Next.js handle the rendering of the 404 page.

### Make Next.js handle any route which starts with `_next` in dev
This prevents dev routes from being caught by user-defined routes.
2018-07-19 21:27:22 +02:00
..
active-class-name #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
basic-css #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
basic-export #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-charset #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-actionhero #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-express #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-fastify improve custom-server-fastify example (#4805) 2018-07-19 21:27:22 +02:00
custom-server-hapi #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-koa #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-micro #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-nodemon #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-polka #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
custom-server-typescript #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
data-fetch #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
form-handler #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
gh-pages #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
head-elements #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
hello-world #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
layout-component #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
nested-components #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
only-client-render-external-dependencies #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
page-transitions Remove page-transitions example 2018-05-25 14:19:40 +02:00
parameterized-routing #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
pass-server-data #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
progressive-render #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
root-static-files #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
shared-modules #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
ssr-caching #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
svg-components #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
using-inferno #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
using-nerv #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
using-preact #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
using-router #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
using-with-router #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-absolute-imports #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-algolia-react-instantsearch #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-amp #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-ant-design #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-ant-design-less #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-antd-mobile #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-aphrodite #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-apollo [with-apollo] simplify apolloState prop (#4755) 2018-07-12 18:59:28 +02:00
with-apollo-and-redux #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-apollo-and-redux-saga #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-apollo-auth #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-app-layout #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-asset-imports #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-babel-macros #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-cerebral #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-componentdidcatch #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-configured-preset-env #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-custom-babel-config #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-custom-reverse-proxy #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-cxs #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-data-prefetch #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-docker #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-dotenv #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-draft-js #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-dynamic-import #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-electron #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-emotion #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-emotion-fiber #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-external-scoped-css Deprecate css examples 2018-01-31 11:19:34 +01:00
with-fela #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-firebase-authentication #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-firebase-hosting #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-firebase-hosting-and-typescript #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-flow #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-freactal #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-glamor #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-glamorous #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-global-stylesheet #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-global-stylesheet-simple #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-google-analytics #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-hashed-statics #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-higher-order-component #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-i18next #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-immutable-redux-wrapper #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-ioc #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-jest #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-jest-typescript #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-kea #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-loading #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-markdown #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-material-ui #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-mobx #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-mobx-state-tree #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-next-css #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-next-less #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-next-page-transitions #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-next-routes #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-next-sass #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-noscript #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-now-env #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-pkg #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-polyfills #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-portals #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-prefetching #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-pretty-url-routing #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-ga #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-helmet #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-i18next #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-intl #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-md #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-native-web [change] Update RNW example (#4787) 2018-07-15 20:59:20 +02:00
with-react-toolbox #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-uwp #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-react-with-styles #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-reasonml #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-rebass #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-recompose #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-redux #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-redux-code-splitting #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-redux-observable #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-redux-reselect-recompose #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-redux-saga #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-redux-wrapper #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-reflux #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-refnux #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-relay-modern #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-rematch #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-scoped-stylesheets-and-postcss Deprecate css examples 2018-01-31 11:19:34 +01:00
with-segment-analytics #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-semantic-ui #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-sentry #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-shallow-routing #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-sitemap-and-robots-express-server #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-socket.io #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-static-export Upgrade serve in with-static-export example (#4800) 2018-07-18 22:16:06 +02:00
with-storybook #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-styled-components #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-styled-jsx-plugins #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-styled-jsx-postcss Make styled-jsx configurable (#3050) 2017-10-15 19:54:57 +02:00
with-styled-jsx-scss #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-styletron #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-sw-precache #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-tailwindcss #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-typescript #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-universal-configuration #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-universal-configuration-runtime #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-unstated #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-url-object-routing #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-videojs #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-webpack-bundle-analyzer #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-webpack-bundle-size-analyzer #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
with-zones #4751 - Explicitly mention install when cloning examples (#4758) 2018-07-11 23:56:15 +02:00
.babelrc Add componentDidCatch example 2018-04-23 14:22:36 -07:00
.gitignore Remove yarn.lock from examples (#912) 2017-01-29 05:48:53 +05:30