Skip to content

Commit 6d0949c

Browse files
committed
update nextjs example to only run on the server
1 parent c5b6710 commit 6d0949c

File tree

4 files changed

+48
-53
lines changed

4 files changed

+48
-53
lines changed

.DS_Store

6 KB
Binary file not shown.

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": [
3-
["@babel/preset-env"],
3+
"@babel/preset-env",
44
"@babel/preset-react"
55
]
66
}

examples/nextjs-cdn/pages/_app.js

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Document, { Head, Main, NextScript } from 'next/document'
2+
import { Resolver } from '../../../';
3+
import CDNAdapter from '../../../cdn';
4+
5+
export default class MyDocument extends Document {
6+
static async getInitialProps({ renderPage }) {
7+
let cssResolver;
8+
const page = renderPage(App => props => {
9+
// assign the resolver to a global variable within getInitialProps
10+
// we need to do this because the only way to get a reference to the page component is by
11+
// calling renderPage which is a sync operation.
12+
// but the resolver is an async operation.
13+
cssResolver = new Resolver(
14+
<App {...props}/>,
15+
new CDNAdapter({
16+
cdnRoot: '/static',
17+
})
18+
);
19+
20+
return (
21+
<App {...props} />
22+
);
23+
});
24+
25+
// get the raw response back from the resolver
26+
const styles = await cssResolver.resolve();
27+
28+
// Create link tags for each style found and merge back into next.js head array.
29+
page.head = [...page.head, ...styles.map(style => {
30+
return <link rel="stylesheet" href={style.path} />
31+
})];
32+
33+
return { ...page };
34+
}
35+
36+
render() {
37+
return (
38+
<html>
39+
<Head />
40+
<body>
41+
<Main />
42+
<NextScript />
43+
</body>
44+
</html>
45+
)
46+
}
47+
}

0 commit comments

Comments
 (0)