Skip to content

Commit 620c8c9

Browse files
committed
Add doc about native Next.js optimisations
1 parent ec66fef commit 620c8c9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/pages/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Next.js pages
2+
3+
[Official documentation](https://nextjs.org/docs/basic-features/pages)
4+
5+
## Native optimisations
6+
7+
Next.js automatically optimise the page client build based on whether they implement `getStaticProps` (SSG) or `getServerSideProps` (SSR).
8+
9+
For instance, all code within `getStaticProps` and `getServerSideProps` is automatically stripped from the browser bundle.
10+
But, it's also the case for top-level `import` that are only used within those functions.
11+
12+
### Visualise bundle optimisation
13+
14+
[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.
15+
16+
Example with:
17+
- [https://next-code-elimination.now.sh/s/hc9SWg_fj]([locale]/examples/pageTemplateSSG)
18+
- [https://next-code-elimination.now.sh/s/M0oIDdQJ2]([locale]/examples/pageTemplateSSR)
19+
- [https://next-code-elimination.now.sh/s/nejeyE9MH]([locale]/examples/native-features/example-with-ssg-and-fallback/[albumId])
20+
21+
> You'll notice for both those files that server-side module imports such as `ApolloQueryResult` are completely stripped from the client-side build.
22+
>
23+
> Also, for `[albumId]`, relative imports such as `songs` are also automatically stripped!

src/pages/[locale]/examples/native-features/example-with-ssg-and-fallback/[albumId].tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import I18nLink from '../../../../../components/i18n/I18nLink';
1212
import DefaultLayout from '../../../../../components/pageLayouts/DefaultLayout';
1313
import ExternalLink from '../../../../../components/utils/ExternalLink';
1414
import withApollo from '../../../../../hocs/withApollo';
15+
import songs from '../../../../../mocks/songs';
1516
import { StaticParams } from '../../../../../types/nextjs/StaticParams';
1617
import { StaticPath } from '../../../../../types/nextjs/StaticPath';
1718
import { StaticPathsOutput } from '../../../../../types/nextjs/StaticPathsOutput';
@@ -44,8 +45,6 @@ export const getStaticProps: GetStaticProps<SSGPageProps, StaticParams> = async
4445
const awaitForMs = getRandomInt(1000, 4000);
4546
await waitFor(awaitForMs);
4647

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

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

0 commit comments

Comments
 (0)