-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
200 lines (187 loc) · 4.15 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React, { useEffect, useState } from "react";
import baseFetch from "isomorphic-unfetch";
import { HttpsAgent } from "agentkeepalive";
import Link from "next/link";
// Supports custom agents!
// This is not required but is for demonstration purposes.
const httpsAgent = new HttpsAgent({});
/**
* Return a `fetch` function and a HAR log that will collect any entries
* created by calling it. In the browser, we skip loading `node-fetch-har`
* completely, because it's meant to apply to `node-fetch` specifically - so
* the base `fetch` will not be wrapped, and the HAR log will be null.
*/
function createFetch(pageInfo) {
if (process.browser) {
return [baseFetch, null];
} else {
// Import `node-fetch-har` here.
const { withHar, createHarLog } = require("../..");
const har = createHarLog([], pageInfo);
const fetch = withHar(baseFetch, { har });
return [fetch, har];
}
}
DemoPage.getInitialProps = async ctx => {
// In practice, you probably want to do this in your `_app.js` so it applies
// to all pages.
const [fetch, harData] = createFetch({ title: "Demo Page" });
await fetch("https://httpstat.us/200", { agent: httpsAgent });
await fetch("https://graphbrainz.herokuapp.com/", {
agent: httpsAgent,
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
query: `
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
`
})
});
return { harData };
};
function useHarUrl(har) {
const [downloadUrl, setDownloadUrl] = useState(null);
useEffect(() => {
if (typeof URL !== "undefined" && URL.createObjectURL) {
const blob = new Blob([JSON.stringify(har)], {
type: "data:application/json;charset=utf-8"
});
const objectUrl = URL.createObjectURL(blob);
setDownloadUrl(objectUrl);
}
}, [har]);
return downloadUrl;
}
export default function DemoPage({ harData }) {
const harUrl = useHarUrl(harData);
return (
<main>
{harUrl ? (
<div>
<p>
Click this to download the HAR file, then drag the file into the
Network tab or another{" "}
<a
href="https://toolbox.googleapps.com/apps/har_analyzer/"
target="_blank"
>
HAR viewer
</a>
!
</p>
<a
href={harUrl}
download="next-ssr-demo.har"
style={{
padding: "10px 20px",
fontFamily: "Lato, sans-serif",
fontWeight: "bold",
fontSize: 13,
textDecoration: "none",
background: "rgb(41, 126, 240)",
color: "white"
}}
>
Download SSR HAR Log
</a>
</div>
) : null}
<p>
<Link href="/">
<a>Send requests in browser</a>
</Link>{" "}
for comparison
</p>
<pre>{JSON.stringify(harData, null, 2)}</pre>
</main>
);
}