-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
37 lines (31 loc) · 1.13 KB
/
utils.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
import { uniqueNamesGenerator, adjectives, animals, NumberDictionary } from 'unique-names-generator';
/*
Get the actual size of a resource downloaded by the browser (e.g. an image) in bytes.
This is supported in recent versions of all major browsers, with some caveats.
See https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/encodedBodySize
*/
export function getResourceSize(url) {
const entry = window?.performance?.getEntriesByName(url)?.[0];
if (entry) {
const size = entry?.encodedBodySize;
return size || undefined;
} else {
return undefined;
}
}
// Note: this only works on the server side
export function getNetlifyContext() {
return process.env.CONTEXT;
}
export function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const uniqueNamesConfig = {
dictionaries: [adjectives, animals],
separator: '-',
length: 2
};
export function uniqueName() {
return uniqueNamesGenerator(uniqueNamesConfig) + "-" + randomInt(100, 999);
}
export const uploadDisabled = process.env.NEXT_PUBLIC_DISABLE_UPLOADS?.toLowerCase() === "true";