Skip to content

Commit

Permalink
Add doc about native Next.js optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadorequest committed May 28, 2020
1 parent ec66fef commit 620c8c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/pages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Next.js pages

[Official documentation](https://nextjs.org/docs/basic-features/pages)

## Native optimisations

Next.js automatically optimise the page client build based on whether they implement `getStaticProps` (SSG) or `getServerSideProps` (SSR).

For instance, all code within `getStaticProps` and `getServerSideProps` is automatically stripped from the browser bundle.
But, it's also the case for top-level `import` that are only used within those functions.

### Visualise bundle optimisation

[https://next-code-elimination.now.sh/](https://next-code-elimination.now.sh/) will help you visualise the difference between the code you write and what's actually bundled into the client.

Example with:
- [https://next-code-elimination.now.sh/s/hc9SWg_fj]([locale]/examples/pageTemplateSSG)
- [https://next-code-elimination.now.sh/s/M0oIDdQJ2]([locale]/examples/pageTemplateSSR)
- [https://next-code-elimination.now.sh/s/nejeyE9MH]([locale]/examples/native-features/example-with-ssg-and-fallback/[albumId])

> You'll notice for both those files that server-side module imports such as `ApolloQueryResult` are completely stripped from the client-side build.
>
> Also, for `[albumId]`, relative imports such as `songs` are also automatically stripped!
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import I18nLink from '../../../../../components/i18n/I18nLink';
import DefaultLayout from '../../../../../components/pageLayouts/DefaultLayout';
import ExternalLink from '../../../../../components/utils/ExternalLink';
import withApollo from '../../../../../hocs/withApollo';
import songs from '../../../../../mocks/songs';
import { StaticParams } from '../../../../../types/nextjs/StaticParams';
import { StaticPath } from '../../../../../types/nextjs/StaticPath';
import { StaticPathsOutput } from '../../../../../types/nextjs/StaticPathsOutput';
Expand Down Expand Up @@ -44,8 +45,6 @@ export const getStaticProps: GetStaticProps<SSGPageProps, StaticParams> = async
const awaitForMs = getRandomInt(1000, 4000);
await waitFor(awaitForMs);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const songs = require('../../../../../mocks/songs').default;
let songId = parseInt(albumId);

if (songId > songs.length - 1) { // Handle overflow
Expand Down Expand Up @@ -161,6 +160,7 @@ const ExampleWithSSGAndFallbackAlbumPage: NextPage<Props> = (props): JSX.Element
background-color: white;
border-radius: 5px;
padding: 30px;
text-align: center;
`}
>
<h1>Album N°{albumId}</h1>
Expand Down

0 comments on commit 620c8c9

Please sign in to comment.