mirror of
https://github.com/SigNoz/signoz.git
synced 2026-12-01 04:30:43 +00:00
Compare commits
2 Commits
main
...
perf/chunk
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
faed0e0a84 | ||
|
|
fa58650fb1 |
46
frontend/boot-guard/bootGuard.css
Normal file
46
frontend/boot-guard/bootGuard.css
Normal file
@@ -0,0 +1,46 @@
|
||||
#signoz-boot-error {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
z-index: 2147483647;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
box-sizing: border-box;
|
||||
text-align: center;
|
||||
font:
|
||||
16px/1.5 system-ui,
|
||||
sans-serif;
|
||||
background: #0b0c0e;
|
||||
color: #e6e6e6;
|
||||
}
|
||||
|
||||
body.lightMode #signoz-boot-error {
|
||||
background: #fff;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
#signoz-boot-error h1 {
|
||||
margin: 0 0 8px;
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
#signoz-boot-error p {
|
||||
margin: 0 0 16px;
|
||||
max-width: 520px;
|
||||
}
|
||||
|
||||
#signoz-boot-error button {
|
||||
padding: 8px 20px;
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
border: 1px solid currentColor;
|
||||
border-radius: 4px;
|
||||
background: transparent;
|
||||
color: inherit;
|
||||
}
|
||||
79
frontend/boot-guard/bootGuard.js
Normal file
79
frontend/boot-guard/bootGuard.js
Normal file
@@ -0,0 +1,79 @@
|
||||
// Inlined into index.html by vite-plugins/inlineBootGuard.ts, not bundled or
|
||||
// transpiled. Keep to ES2015, so a browser too old for the app can still show
|
||||
// the message.
|
||||
(function () {
|
||||
const MESSAGES = {
|
||||
unsupported: {
|
||||
title: 'Your browser is not supported',
|
||||
text:
|
||||
'SigNoz needs a newer browser. Update Chrome, Edge, Firefox or Safari, then reload this page.',
|
||||
},
|
||||
load: {
|
||||
title: 'SigNoz failed to load',
|
||||
text:
|
||||
'Part of the app could not be downloaded. Check your connection and reload the page. If it keeps happening, contact your administrator.',
|
||||
},
|
||||
};
|
||||
|
||||
function createElement(tag, text) {
|
||||
const element = document.createElement(tag);
|
||||
element.textContent = text;
|
||||
return element;
|
||||
}
|
||||
|
||||
function render(kind) {
|
||||
if (document.getElementById('signoz-boot-error')) {
|
||||
return;
|
||||
}
|
||||
const message = MESSAGES[kind];
|
||||
const box = document.createElement('div');
|
||||
box.id = 'signoz-boot-error';
|
||||
box.setAttribute('role', 'alert');
|
||||
box.setAttribute('data-testid', 'boot-error');
|
||||
box.appendChild(createElement('h1', message.title));
|
||||
box.appendChild(createElement('p', message.text));
|
||||
if (kind === 'load') {
|
||||
const reload = createElement('button', 'Reload');
|
||||
reload.type = 'button';
|
||||
reload.setAttribute('data-testid', 'boot-error-reload');
|
||||
reload.onclick = function () {
|
||||
window.location.reload();
|
||||
};
|
||||
box.appendChild(reload);
|
||||
}
|
||||
document.body.appendChild(box);
|
||||
}
|
||||
|
||||
function renderWhenReady(kind) {
|
||||
if (document.body) {
|
||||
render(kind);
|
||||
} else {
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
render(kind);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const supportsImportMap = !!(
|
||||
window.HTMLScriptElement &&
|
||||
HTMLScriptElement.supports &&
|
||||
HTMLScriptElement.supports('importmap')
|
||||
);
|
||||
|
||||
if (!supportsImportMap) {
|
||||
renderWhenReady('unsupported');
|
||||
}
|
||||
|
||||
// A module script fires `error` when any file of its static import graph
|
||||
// fails to download. Without import maps, every chunk key is a 404.
|
||||
window.addEventListener(
|
||||
'error',
|
||||
function (event) {
|
||||
const target = event.target;
|
||||
if (target && target.tagName === 'SCRIPT' && target.type === 'module') {
|
||||
renderWhenReady(supportsImportMap ? 'load' : 'unsupported');
|
||||
}
|
||||
},
|
||||
true,
|
||||
);
|
||||
})();
|
||||
@@ -42,7 +42,10 @@ const config: Config.InitialOptions = {
|
||||
'<rootDir>/node_modules/@signozhq/$1/dist/$1.js',
|
||||
},
|
||||
extensionsToTreatAsEsm: ['.ts'],
|
||||
testMatch: ['<rootDir>/src/**/*?(*.)(test).(ts|js)?(x)'],
|
||||
testMatch: [
|
||||
'<rootDir>/src/**/*?(*.)(test).(ts|js)?(x)',
|
||||
'<rootDir>/vite-plugins/**/*.test.ts',
|
||||
],
|
||||
preset: 'ts-jest/presets/js-with-ts-esm',
|
||||
transform: {
|
||||
'^.+\\.(ts|tsx)?$': [
|
||||
|
||||
@@ -83,6 +83,7 @@
|
||||
"./jest.config.ts",
|
||||
"./jest.setup.ts",
|
||||
"./vite.config.ts",
|
||||
"./vite-plugins",
|
||||
"./commitlint.config.ts"
|
||||
]
|
||||
}
|
||||
|
||||
84
frontend/vite-plugins/__tests__/bootGuard.test.ts
Normal file
84
frontend/vite-plugins/__tests__/bootGuard.test.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import { screen } from '@testing-library/react';
|
||||
|
||||
const guard = readFileSync(
|
||||
resolve(__dirname, '../../boot-guard/bootGuard.js'),
|
||||
'utf8',
|
||||
);
|
||||
|
||||
function runGuard(supportsImportMap: boolean): void {
|
||||
document.head.innerHTML = '';
|
||||
document.body.innerHTML = '';
|
||||
Object.assign(HTMLScriptElement, {
|
||||
supports: (type: string) => supportsImportMap && type === 'importmap',
|
||||
});
|
||||
|
||||
const script = document.createElement('script');
|
||||
script.textContent = guard;
|
||||
document.body.appendChild(script);
|
||||
}
|
||||
|
||||
function addScript(type: string): HTMLScriptElement {
|
||||
const script = document.createElement('script');
|
||||
script.type = type;
|
||||
document.head.appendChild(script);
|
||||
return script;
|
||||
}
|
||||
|
||||
describe('boot guard', () => {
|
||||
let removeListeners: () => void = () => {};
|
||||
|
||||
beforeEach(() => {
|
||||
const added: Parameters<typeof window.addEventListener>[] = [];
|
||||
const addEventListener = window.addEventListener.bind(window);
|
||||
jest
|
||||
.spyOn(window, 'addEventListener')
|
||||
.mockImplementation((...args: Parameters<typeof addEventListener>) => {
|
||||
added.push(args);
|
||||
addEventListener(...args);
|
||||
});
|
||||
removeListeners = (): void => {
|
||||
added.forEach((args) => window.removeEventListener(...args));
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
removeListeners();
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('shows nothing while the app loads', () => {
|
||||
runGuard(true);
|
||||
|
||||
expect(screen.queryByTestId('boot-error')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the load error when the entry module fails', () => {
|
||||
runGuard(true);
|
||||
|
||||
addScript('module').dispatchEvent(new Event('error'));
|
||||
|
||||
expect(screen.getByTestId('boot-error')).toHaveTextContent(
|
||||
'SigNoz failed to load',
|
||||
);
|
||||
expect(screen.getByTestId('boot-error-reload')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('ignores errors from classic scripts', () => {
|
||||
runGuard(true);
|
||||
|
||||
addScript('text/javascript').dispatchEvent(new Event('error'));
|
||||
|
||||
expect(screen.queryByTestId('boot-error')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('asks for a newer browser when import maps are not supported', () => {
|
||||
runGuard(false);
|
||||
|
||||
expect(screen.getByTestId('boot-error')).toHaveTextContent(
|
||||
'Your browser is not supported',
|
||||
);
|
||||
expect(screen.queryByTestId('boot-error-reload')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
29
frontend/vite-plugins/__tests__/inlineBootGuard.test.ts
Normal file
29
frontend/vite-plugins/__tests__/inlineBootGuard.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import type { HtmlTagDescriptor } from 'vite';
|
||||
|
||||
import { inlineBootGuard } from '../inlineBootGuard';
|
||||
|
||||
function readBootGuardFile(name: string): string {
|
||||
return readFileSync(resolve(__dirname, '../../boot-guard', name), 'utf8');
|
||||
}
|
||||
|
||||
describe('inlineBootGuard', () => {
|
||||
it('inlines the boot guard style and script into the head', () => {
|
||||
const transform = inlineBootGuard()
|
||||
.transformIndexHtml as () => HtmlTagDescriptor[];
|
||||
|
||||
expect(transform()).toStrictEqual([
|
||||
{
|
||||
tag: 'style',
|
||||
children: readBootGuardFile('bootGuard.css'),
|
||||
injectTo: 'head',
|
||||
},
|
||||
{
|
||||
tag: 'script',
|
||||
children: readBootGuardFile('bootGuard.js'),
|
||||
injectTo: 'head',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
36
frontend/vite-plugins/__tests__/vendorChunkName.test.ts
Normal file
36
frontend/vite-plugins/__tests__/vendorChunkName.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { vendorChunkName } from '../vendorChunkName';
|
||||
|
||||
describe('vendorChunkName', () => {
|
||||
it('names an unscoped package after its folder', () => {
|
||||
expect(
|
||||
vendorChunkName('/repo/node_modules/react-dom/cjs/react-dom.production.js'),
|
||||
).toBe('v-react-dom');
|
||||
});
|
||||
|
||||
it('joins the scope and the name of a scoped package', () => {
|
||||
expect(
|
||||
vendorChunkName(
|
||||
'/repo/node_modules/@tanstack/react-table/build/lib/index.mjs',
|
||||
),
|
||||
).toBe('v--tanstack-react-table');
|
||||
});
|
||||
|
||||
it('uses the innermost node_modules of a pnpm path', () => {
|
||||
expect(
|
||||
vendorChunkName(
|
||||
'/repo/node_modules/.pnpm/antd@5.0.0_react@18/node_modules/antd/es/index.js',
|
||||
),
|
||||
).toBe('v-antd');
|
||||
});
|
||||
|
||||
it('returns null for app code and virtual modules', () => {
|
||||
expect(vendorChunkName('/repo/src/pages/Home/index.tsx')).toBeNull();
|
||||
expect(vendorChunkName('\0vite/preload-helper.js')).toBeNull();
|
||||
});
|
||||
|
||||
it('replaces characters that are not safe in a file name', () => {
|
||||
expect(vendorChunkName('/repo/node_modules/@scope/a+b/index.js')).toBe(
|
||||
'v--scope-a-b',
|
||||
);
|
||||
});
|
||||
});
|
||||
29
frontend/vite-plugins/inlineBootGuard.ts
Normal file
29
frontend/vite-plugins/inlineBootGuard.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import type { HtmlTagDescriptor, Plugin } from 'vite';
|
||||
|
||||
const BOOT_GUARD_DIR = resolve(__dirname, '../boot-guard');
|
||||
|
||||
function readBootGuardFile(name: string): string {
|
||||
return readFileSync(resolve(BOOT_GUARD_DIR, name), 'utf8');
|
||||
}
|
||||
|
||||
export function inlineBootGuard(): Plugin {
|
||||
return {
|
||||
name: 'signoz:inline-boot-guard',
|
||||
transformIndexHtml(): HtmlTagDescriptor[] {
|
||||
return [
|
||||
{
|
||||
tag: 'style',
|
||||
children: readBootGuardFile('bootGuard.css'),
|
||||
injectTo: 'head',
|
||||
},
|
||||
{
|
||||
tag: 'script',
|
||||
children: readBootGuardFile('bootGuard.js'),
|
||||
injectTo: 'head',
|
||||
},
|
||||
];
|
||||
},
|
||||
};
|
||||
}
|
||||
13
frontend/vite-plugins/vendorChunkName.ts
Normal file
13
frontend/vite-plugins/vendorChunkName.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
const NODE_MODULES = 'node_modules/';
|
||||
|
||||
export function vendorChunkName(moduleId: string): string | null {
|
||||
const start = moduleId.lastIndexOf(NODE_MODULES);
|
||||
if (start < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [first, second] = moduleId.slice(start + NODE_MODULES.length).split('/');
|
||||
const packageName = first.startsWith('@') ? `${first}-${second}` : first;
|
||||
|
||||
return `v-${packageName.replace(/[^A-Za-z0-9_.-]/g, '-')}`;
|
||||
}
|
||||
@@ -14,6 +14,8 @@ import type {
|
||||
Sentry,
|
||||
WebSettings,
|
||||
} from 'types/generated/webSettings';
|
||||
import { inlineBootGuard } from './vite-plugins/inlineBootGuard';
|
||||
import { vendorChunkName } from './vite-plugins/vendorChunkName';
|
||||
|
||||
// In dev the Go backend is not involved, so replace the [[.BaseHref]] placeholder
|
||||
// with the configured base path so relative assets resolve correctly from the Vite dev server.
|
||||
@@ -84,6 +86,7 @@ export default defineConfig(({ mode }): UserConfig => {
|
||||
rawMarkdownPlugin(),
|
||||
devBasePathPlugin(basePath),
|
||||
devBootDataPlugin(env),
|
||||
inlineBootGuard(),
|
||||
react(),
|
||||
vitePluginChecker({
|
||||
typescript: true,
|
||||
@@ -112,7 +115,13 @@ export default defineConfig(({ mode }): UserConfig => {
|
||||
project: env.VITE_SENTRY_PROJECT_ID,
|
||||
// Pin the sourcemap-upload release to the same value injected as
|
||||
// process.env.VERSION so uploaded sourcemaps resolve. Ref: platform-pod#2393
|
||||
release: { name: env.VITE_VERSION, setCommits: { auto: true } },
|
||||
// `inject: false`: the release string would otherwise land in every chunk
|
||||
// and change all their hashes on each release. Sentry.init sets it.
|
||||
release: {
|
||||
name: env.VITE_VERSION,
|
||||
inject: false,
|
||||
setCommits: { auto: true },
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -187,12 +196,33 @@ export default defineConfig(({ mode }): UserConfig => {
|
||||
sourcemap: true,
|
||||
outDir: 'build',
|
||||
cssMinify: 'esbuild',
|
||||
chunkImportMap: true,
|
||||
rolldownOptions: {
|
||||
output: {
|
||||
// Without this group Rolldown splits the entry's static graph into ~300
|
||||
// chunks, 324 requests on first load.
|
||||
// Export aliases are renumbered whenever a chunk's exports change, which
|
||||
// re-hashes every importer.
|
||||
minifyInternalExports: false,
|
||||
// Without these groups Rolldown splits the entry's static graph into ~300
|
||||
// chunks, 324 requests on first load. `initial` keeps the app code of that
|
||||
// graph in one chunk, so the packages around it can keep their hashes.
|
||||
codeSplitting: {
|
||||
groups: [{ name: 'initial', tags: ['$initial'] }],
|
||||
groups: [
|
||||
{
|
||||
name: vendorChunkName,
|
||||
debugName: 'per-package',
|
||||
test: 'node_modules',
|
||||
tags: ['$initial'],
|
||||
minSize: 100 * 1024,
|
||||
priority: 5,
|
||||
},
|
||||
{
|
||||
name: 'v-misc',
|
||||
test: 'node_modules',
|
||||
tags: ['$initial'],
|
||||
priority: 1,
|
||||
},
|
||||
{ name: 'initial', tags: ['$initial'] },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user