-
Notifications
You must be signed in to change notification settings - Fork 31
/
_app.js
113 lines (94 loc) · 3.09 KB
/
_app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* global process */
import { NodeContext, djedi } from "djedi-react";
import unfetch from "isomorphic-unfetch";
import App, { Container } from "next/app";
import PropTypes from "prop-types";
import React from "react";
import Link from "../components/Link";
const LANGUAGES = {
"en-us": "English",
"de-de": "German",
"sv-se": "Swedish",
};
const DEFAULT_LANGUAGE = "en-us";
// Set default and available languages.
djedi.options.languages = {
default: DEFAULT_LANGUAGE,
additional: Object.keys(LANGUAGES).filter(
language => language !== DEFAULT_LANGUAGE
),
};
// Set baseUrl differently for server and browser rendering.
djedi.options.baseUrl =
(typeof process !== "undefined" && process.env.SERVER_BASE_URL) ||
"http://localhost:8000/djedi/api";
// Required: Set the `fetch` function to use.
djedi.options.fetch = unfetch;
// Inject the admin sidebar, if the user has permission.
djedi.injectAdmin();
export default class MyApp extends App {
static async getInitialProps({ Component, ctx, ctx: { query } }) {
// This demo uses a query parameter for the language to keep the demo small.
// Ugly, but it works.
const language = {}.hasOwnProperty.call(LANGUAGES, query.language)
? query.language
: DEFAULT_LANGUAGE;
// Load page data and Djedi nodes in parallel.
const [pageProps] = await Promise.all([
// Pass the language to the child `getInitialProps`, in case it needs to
// call `djedi.prefetch`.
Component.getInitialProps
? Component.getInitialProps({ ...ctx, language })
: {},
// Prefetch on all pages. If the page takes care of prefetching itself (like
// index.js does), the page should set `.skipDjediPrefetch = true` on
// itself to avoid double network requests.
Component.skipDjediPrefetch ? undefined : djedi.prefetch({ language }),
]);
// Track which nodes are actually rendered.
const nodes = djedi.track();
return { pageProps, nodes, language };
}
// Add in nodes loaded by `djedi.track` server-side.
constructor(props) {
super(props);
if (props.nodes != null) {
djedi.addNodes(props.nodes);
}
}
render() {
const { Component, pageProps, language } = this.props;
return (
<React.StrictMode>
<Container>
{/* Provide the current language to all `<Node>`s. */}
<NodeContext.Provider value={language}>
<LanguageChooser current={language} />
<Component {...pageProps} />
</NodeContext.Provider>
</Container>
</React.StrictMode>
);
}
}
LanguageChooser.propTypes = {
current: PropTypes.oneOf(Object.keys(LANGUAGES)).isRequired,
};
function LanguageChooser({ current }) {
return (
<div>
{Object.entries(LANGUAGES).map(([language, name], index) => (
<React.Fragment key={language}>
{index !== 0 && " / "}
{language === current ? (
<strong>{name}</strong>
) : (
<Link href={{ query: { language } }}>
<a>{name}</a>
</Link>
)}
</React.Fragment>
))}
</div>
);
}