-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_document.tsx
48 lines (44 loc) · 1.06 KB
/
_document.tsx
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
//@ts-nocheck
import React from 'react';
import Document, {
Html,
Head,
Main,
NextScript,
DocumentContext,
} from 'next/document';
import {getLangFromReq} from '../src/utility/fromReq';
interface langType {
lang: string;
}
const scriptTxt: string = `
(function () {
const { pathname } = window.location
const ipfsMatch = /.*\\/Qm\\w{44}\\//.exec(pathname)
const base = document.createElement('base')
base.href = ipfsMatch ? ipfsMatch[0] : '/'
document.head.append(base)
})();
`;
class MyDocument extends Document<langType> {
static async getInitialProps(ctx: DocumentContext) {
const initialProps = await Document.getInitialProps(ctx);
const lang = getLangFromReq(ctx.req);
return {...initialProps, lang};
}
render(): JSX.Element {
const {lang}: langType = this.props;
return (
<Html lang={lang}>
<Head>
<script dangerouslySetInnerHTML={{__html: scriptTxt}}/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;