forked from baron/baron-sso
fix(userfront): prevent public env asset request
This commit is contained in:
@@ -13,6 +13,10 @@ if rg -n "FontLoader|assets/fonts/NotoSansKR|_loadBundledFonts" userfront/lib us
|
|||||||
fail "userfront must not block first render on bundled NotoSansKR font loading"
|
fail "userfront must not block first render on bundled NotoSansKR font loading"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if rg -n "dotenv\.load|touch \.env" userfront/lib/main.dart userfront/Dockerfile; then
|
||||||
|
fail "userfront web startup must not request or create public .env assets"
|
||||||
|
fi
|
||||||
|
|
||||||
if rg -n "fontFamily:\s*['\"]NotoSansKR['\"]" userfront/lib; then
|
if rg -n "fontFamily:\s*['\"]NotoSansKR['\"]" userfront/lib; then
|
||||||
fail "userfront theme must use the platform default font"
|
fail "userfront theme must use the platform default font"
|
||||||
fi
|
fi
|
||||||
@@ -34,6 +38,9 @@ rg -q "nginx-mod-http-brotli" userfront/Dockerfile || fail "runtime image must i
|
|||||||
rg -Fq "main\\.dart\\.[0-9a-f]{12}" userfront/nginx.conf || fail "hashed app entrypoints must use immutable cache"
|
rg -Fq "main\\.dart\\.[0-9a-f]{12}" userfront/nginx.conf || fail "hashed app entrypoints must use immutable cache"
|
||||||
rg -q "brotli_static\s+on;" userfront/nginx.conf || fail "nginx must serve pre-compressed brotli assets"
|
rg -q "brotli_static\s+on;" userfront/nginx.conf || fail "nginx must serve pre-compressed brotli assets"
|
||||||
rg -q "brotliCompressSync" userfront/scripts/optimize-web-build.mjs || fail "Docker build optimization must generate brotli assets"
|
rg -q "brotliCompressSync" userfront/scripts/optimize-web-build.mjs || fail "Docker build optimization must generate brotli assets"
|
||||||
|
rg -q "modulepreload" userfront/scripts/optimize-web-build.mjs || fail "Docker build optimization must preload wasm module entrypoints"
|
||||||
|
rg -q "canvasKitBaseUrl:\"canvaskit/\"" userfront/scripts/optimize-web-build.mjs || fail "userfront web build must force local CanvasKit instead of fetching engine resources from a CDN"
|
||||||
|
rg -q "_flutter\.loader\.load\(\{config:\{canvasKitBaseUrl:\"canvaskit/\"\}\}\);" userfront/scripts/optimize-web-build.mjs || fail "Flutter service worker registration must be removed from cold path"
|
||||||
if rg -n "gzip|gzipSync|\\.gz" userfront/nginx.conf userfront/scripts/optimize-web-build.mjs; then
|
if rg -n "gzip|gzipSync|\\.gz" userfront/nginx.conf userfront/scripts/optimize-web-build.mjs; then
|
||||||
fail "userfront web compression must be managed as brotli-only"
|
fail "userfront web compression must be managed as brotli-only"
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -35,6 +35,13 @@ const contentTypes = {
|
|||||||
const server = createServer((req, res) => {
|
const server = createServer((req, res) => {
|
||||||
const url = new URL(req.url ?? '/', 'http://localhost');
|
const url = new URL(req.url ?? '/', 'http://localhost');
|
||||||
const pathname = decodeURIComponent(url.pathname);
|
const pathname = decodeURIComponent(url.pathname);
|
||||||
|
if (pathname === '/') {
|
||||||
|
res.statusCode = 302;
|
||||||
|
res.setHeader('Location', '/ko/signin');
|
||||||
|
res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate');
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const relative = pathname === '/' ? '/index.html' : pathname;
|
const relative = pathname === '/' ? '/index.html' : pathname;
|
||||||
const candidate = normalize(join(root, relative));
|
const candidate = normalize(join(root, relative));
|
||||||
|
|
||||||
@@ -48,6 +55,13 @@ const server = createServer((req, res) => {
|
|||||||
let servesAppShellFallback = false;
|
let servesAppShellFallback = false;
|
||||||
|
|
||||||
if (!existsSync(filePath) || statSync(filePath).isDirectory()) {
|
if (!existsSync(filePath) || statSync(filePath).isDirectory()) {
|
||||||
|
if (extname(pathname)) {
|
||||||
|
res.statusCode = 404;
|
||||||
|
res.setHeader('Cache-Control', 'no-cache, max-age=0, must-revalidate');
|
||||||
|
res.end('Not Found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Flutter web 라우팅 경로(`/ko`, `/ko/signin`)도 index.html로 fallback 처리
|
// Flutter web 라우팅 경로(`/ko`, `/ko/signin`)도 index.html로 fallback 처리
|
||||||
filePath = join(root, 'index.html');
|
filePath = join(root, 'index.html');
|
||||||
servesAppShellFallback = true;
|
servesAppShellFallback = true;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ type LoadMetrics = {
|
|||||||
durationMs: number;
|
durationMs: number;
|
||||||
transferredBytes: number;
|
transferredBytes: number;
|
||||||
requestedUrls: string[];
|
requestedUrls: string[];
|
||||||
|
requestedPathCounts: Map<string, number>;
|
||||||
cacheControlByPath: Map<string, string>;
|
cacheControlByPath: Map<string, string>;
|
||||||
contentEncodingByPath: Map<string, string>;
|
contentEncodingByPath: Map<string, string>;
|
||||||
};
|
};
|
||||||
@@ -30,15 +31,24 @@ async function mockPublicApis(page: Page): Promise<void> {
|
|||||||
|
|
||||||
async function measureSigninLoad(page: Page): Promise<LoadMetrics> {
|
async function measureSigninLoad(page: Page): Promise<LoadMetrics> {
|
||||||
const requestedUrls: string[] = [];
|
const requestedUrls: string[] = [];
|
||||||
|
const requestedPathCounts = new Map<string, number>();
|
||||||
const cacheControlByPath = new Map<string, string>();
|
const cacheControlByPath = new Map<string, string>();
|
||||||
const contentEncodingByPath = new Map<string, string>();
|
const contentEncodingByPath = new Map<string, string>();
|
||||||
let transferredBytes = 0;
|
let transferredBytes = 0;
|
||||||
|
|
||||||
page.on('request', (request: Request) => {
|
const onRequest = (request: Request) => {
|
||||||
|
const requestUrl = new URL(request.url());
|
||||||
requestedUrls.push(request.url());
|
requestedUrls.push(request.url());
|
||||||
});
|
if (requestUrl.protocol === 'http:' || requestUrl.protocol === 'https:') {
|
||||||
|
const resourceKey = `${requestUrl.origin}${requestUrl.pathname}`;
|
||||||
|
requestedPathCounts.set(
|
||||||
|
resourceKey,
|
||||||
|
(requestedPathCounts.get(resourceKey) ?? 0) + 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
page.on('response', async (response) => {
|
const onResponse = async (response) => {
|
||||||
const url = new URL(response.url());
|
const url = new URL(response.url());
|
||||||
const cacheControl = response.headers()['cache-control'];
|
const cacheControl = response.headers()['cache-control'];
|
||||||
if (cacheControl) {
|
if (cacheControl) {
|
||||||
@@ -54,20 +64,44 @@ async function measureSigninLoad(page: Page): Promise<LoadMetrics> {
|
|||||||
const sizes = await response.request().sizes().catch(() => null);
|
const sizes = await response.request().sizes().catch(() => null);
|
||||||
transferredBytes += sizes?.responseBodySize ?? 0;
|
transferredBytes += sizes?.responseBodySize ?? 0;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const start = performance.now();
|
|
||||||
await page.goto('/ko/signin', { waitUntil: 'networkidle' });
|
|
||||||
await expect(page).toHaveURL(/\/ko\/signin(?:\?.*)?$/);
|
|
||||||
const durationMs = Math.round(performance.now() - start);
|
|
||||||
|
|
||||||
return {
|
|
||||||
durationMs,
|
|
||||||
transferredBytes,
|
|
||||||
requestedUrls,
|
|
||||||
cacheControlByPath,
|
|
||||||
contentEncodingByPath,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
page.on('request', onRequest);
|
||||||
|
page.on('response', onResponse);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const start = performance.now();
|
||||||
|
await page.goto('/ko/signin', { waitUntil: 'networkidle' });
|
||||||
|
await expect(page).toHaveURL(/\/ko\/signin(?:\?.*)?$/);
|
||||||
|
const durationMs = Math.round(performance.now() - start);
|
||||||
|
|
||||||
|
return {
|
||||||
|
durationMs,
|
||||||
|
transferredBytes,
|
||||||
|
requestedUrls,
|
||||||
|
requestedPathCounts,
|
||||||
|
cacheControlByPath,
|
||||||
|
contentEncodingByPath,
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
page.off('request', onRequest);
|
||||||
|
page.off('response', onResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectNoDuplicateStaticRequests(metrics: LoadMetrics): void {
|
||||||
|
const duplicates = [...metrics.requestedPathCounts.entries()].filter(
|
||||||
|
([resourceKey, count]) => {
|
||||||
|
const path = new URL(resourceKey).pathname;
|
||||||
|
return (
|
||||||
|
count > 1 &&
|
||||||
|
!path.startsWith('/api/') &&
|
||||||
|
!path.endsWith('/ko/signin') &&
|
||||||
|
!path.endsWith('/')
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(duplicates).toEqual([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
test.describe('UserFront login performance budget', () => {
|
test.describe('UserFront login performance budget', () => {
|
||||||
@@ -82,8 +116,11 @@ test.describe('UserFront login performance budget', () => {
|
|||||||
`[userfront-perf] cold=${cold.durationMs}ms/${cold.transferredBytes}B warm=${warm.durationMs}ms/${warm.transferredBytes}B`,
|
`[userfront-perf] cold=${cold.durationMs}ms/${cold.transferredBytes}B warm=${warm.durationMs}ms/${warm.transferredBytes}B`,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(warm.durationMs).toBeLessThanOrEqual(2000);
|
expect(cold.durationMs).toBeLessThanOrEqual(1500);
|
||||||
|
expect(warm.durationMs).toBeLessThanOrEqual(1100);
|
||||||
expect(warm.transferredBytes).toBeLessThanOrEqual(1_000_000);
|
expect(warm.transferredBytes).toBeLessThanOrEqual(1_000_000);
|
||||||
|
expectNoDuplicateStaticRequests(cold);
|
||||||
|
expectNoDuplicateStaticRequests(warm);
|
||||||
expect(warm.requestedUrls.some((url) => url.includes('NotoSansKR'))).toBe(
|
expect(warm.requestedUrls.some((url) => url.includes('NotoSansKR'))).toBe(
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
@@ -92,6 +129,19 @@ test.describe('UserFront login performance budget', () => {
|
|||||||
url.includes('fonts.googleapis.com/icon?family=Material+Icons'),
|
url.includes('fonts.googleapis.com/icon?family=Material+Icons'),
|
||||||
),
|
),
|
||||||
).toBe(false);
|
).toBe(false);
|
||||||
|
expect(
|
||||||
|
[...cold.requestedUrls, ...warm.requestedUrls].some((url) =>
|
||||||
|
url.includes('www.gstatic.com/flutter-canvaskit'),
|
||||||
|
),
|
||||||
|
).toBe(false);
|
||||||
|
expect(warm.requestedUrls.some((url) => url.endsWith('/assets/.env'))).toBe(
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
cold.requestedUrls.some((url) =>
|
||||||
|
url.endsWith('/flutter_service_worker.js'),
|
||||||
|
),
|
||||||
|
).toBe(false);
|
||||||
|
|
||||||
const cacheControlByPath = new Map([
|
const cacheControlByPath = new Map([
|
||||||
...cold.cacheControlByPath,
|
...cold.cacheControlByPath,
|
||||||
@@ -112,5 +162,32 @@ test.describe('UserFront login performance budget', () => {
|
|||||||
encoding === 'br',
|
encoding === 'br',
|
||||||
);
|
);
|
||||||
expect(brotliEntrypoint).toBe(true);
|
expect(brotliEntrypoint).toBe(true);
|
||||||
|
expect(contentEncodingByPath.get('/canvaskit/skwasm.wasm')).toBe('br');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('root redirects to localized signin before Flutter boots', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
await mockPublicApis(page);
|
||||||
|
|
||||||
|
const requestedUrls: string[] = [];
|
||||||
|
page.on('request', (request) => {
|
||||||
|
requestedUrls.push(request.url());
|
||||||
|
});
|
||||||
|
|
||||||
|
const start = performance.now();
|
||||||
|
await page.goto('/', { waitUntil: 'domcontentloaded' });
|
||||||
|
await expect(page).toHaveURL(/\/ko\/signin(?:\?.*)?$/);
|
||||||
|
const durationMs = Math.round(performance.now() - start);
|
||||||
|
|
||||||
|
expect(durationMs).toBeLessThanOrEqual(300);
|
||||||
|
const rootIndex = requestedUrls.findIndex(
|
||||||
|
(url) => new URL(url).pathname === '/',
|
||||||
|
);
|
||||||
|
const bootstrapIndex = requestedUrls.findIndex((url) =>
|
||||||
|
new URL(url).pathname.endsWith('/flutter_bootstrap.js'),
|
||||||
|
);
|
||||||
|
expect(rootIndex).toBeGreaterThanOrEqual(0);
|
||||||
|
expect(bootstrapIndex).toBeGreaterThan(rootIndex);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ COPY . .
|
|||||||
RUN /bin/sh ./scripts/sync_userfront_locales.sh
|
RUN /bin/sh ./scripts/sync_userfront_locales.sh
|
||||||
WORKDIR /app/userfront
|
WORKDIR /app/userfront
|
||||||
RUN flutter pub get
|
RUN flutter pub get
|
||||||
RUN touch .env
|
|
||||||
RUN flutter build web --release --wasm
|
RUN flutter build web --release --wasm
|
||||||
|
|
||||||
FROM node:24-alpine AS optimize
|
FROM node:24-alpine AS optimize
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import 'dart:convert';
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
||||||
import 'package:easy_localization/easy_localization.dart' hide tr;
|
import 'package:easy_localization/easy_localization.dart' hide tr;
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
||||||
@@ -189,13 +188,6 @@ void main() async {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// .env가 없더라도 초기화 상태를 보장하도록 optional 로딩
|
|
||||||
try {
|
|
||||||
await dotenv.load(fileName: ".env", isOptional: true);
|
|
||||||
} catch (e) {
|
|
||||||
_log.warning("Warning: .env file load failed: $e");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 0. Initialize Logger
|
// 0. Initialize Logger
|
||||||
LoggerService.init();
|
LoggerService.init();
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,10 @@ server {
|
|||||||
|
|
||||||
# --- UserFront Static Files ---
|
# --- UserFront Static Files ---
|
||||||
|
|
||||||
|
location = / {
|
||||||
|
return 302 /ko/signin;
|
||||||
|
}
|
||||||
|
|
||||||
# App shell and Flutter bootstrap files must revalidate on each deployment.
|
# App shell and Flutter bootstrap files must revalidate on each deployment.
|
||||||
location = /index.html {
|
location = /index.html {
|
||||||
add_header Cache-Control "no-cache, max-age=0, must-revalidate";
|
add_header Cache-Control "no-cache, max-age=0, must-revalidate";
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
import { brotliCompressSync, constants } from 'node:zlib';
|
import { brotliCompressSync, constants } from 'node:zlib';
|
||||||
import { createHash } from 'node:crypto';
|
import { createHash } from 'node:crypto';
|
||||||
import { existsSync, readFileSync, readdirSync, renameSync, writeFileSync } from 'node:fs';
|
import {
|
||||||
|
existsSync,
|
||||||
|
readFileSync,
|
||||||
|
readdirSync,
|
||||||
|
renameSync,
|
||||||
|
unlinkSync,
|
||||||
|
writeFileSync,
|
||||||
|
} from 'node:fs';
|
||||||
import { basename, extname, join } from 'node:path';
|
import { basename, extname, join } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
@@ -9,7 +16,19 @@ const __filename = fileURLToPath(import.meta.url);
|
|||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
const buildDir = process.argv[2] ?? join(__dirname, '..', 'build', 'web');
|
const buildDir = process.argv[2] ?? join(__dirname, '..', 'build', 'web');
|
||||||
const bootstrapPath = join(buildDir, 'flutter_bootstrap.js');
|
const bootstrapPath = join(buildDir, 'flutter_bootstrap.js');
|
||||||
|
const indexPath = join(buildDir, 'index.html');
|
||||||
const hashableEntrypoints = ['main.dart.js', 'main.dart.mjs', 'main.dart.wasm'];
|
const hashableEntrypoints = ['main.dart.js', 'main.dart.mjs', 'main.dart.wasm'];
|
||||||
|
const loginFontFallbackPreloads = [
|
||||||
|
'https://fonts.gstatic.com/s/roboto/v32/KFOmCnqEu92Fr1Me4GZLCzYlKw.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.110.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.113.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.114.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.115.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.116.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.117.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.118.woff2',
|
||||||
|
'https://fonts.gstatic.com/s/notosanskr/v36/PbyxFmXiEBPT4ITbgNA5Cgms3VYcOA-vvnIzzuoyeLGC5nwuDo-KBTUm6CryotyJROlrnQ.119.woff2',
|
||||||
|
];
|
||||||
const compressibleExtensions = new Set([
|
const compressibleExtensions = new Set([
|
||||||
'.css',
|
'.css',
|
||||||
'.html',
|
'.html',
|
||||||
@@ -26,9 +45,16 @@ if (!existsSync(bootstrapPath)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let bootstrap = readFileSync(bootstrapPath, 'utf8');
|
let bootstrap = readFileSync(bootstrapPath, 'utf8');
|
||||||
|
const hashedEntrypoints = new Map();
|
||||||
|
const activeEntrypointFiles = new Set();
|
||||||
|
|
||||||
for (const entrypoint of hashableEntrypoints) {
|
for (const entrypoint of hashableEntrypoints) {
|
||||||
const sourcePath = join(buildDir, entrypoint);
|
const sourceName = findEntrypointSource(entrypoint, bootstrap);
|
||||||
|
if (!sourceName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sourcePath = join(buildDir, sourceName);
|
||||||
if (!existsSync(sourcePath)) {
|
if (!existsSync(sourcePath)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -40,12 +66,80 @@ for (const entrypoint of hashableEntrypoints) {
|
|||||||
const hashedName = `${stem}.${hash}${extension}`;
|
const hashedName = `${stem}.${hash}${extension}`;
|
||||||
const targetPath = join(buildDir, hashedName);
|
const targetPath = join(buildDir, hashedName);
|
||||||
|
|
||||||
renameSync(sourcePath, targetPath);
|
if (sourceName !== hashedName) {
|
||||||
|
renameSync(sourcePath, targetPath);
|
||||||
|
}
|
||||||
|
bootstrap = bootstrap.replaceAll(sourceName, hashedName);
|
||||||
bootstrap = bootstrap.replaceAll(entrypoint, hashedName);
|
bootstrap = bootstrap.replaceAll(entrypoint, hashedName);
|
||||||
|
hashedEntrypoints.set(entrypoint, hashedName);
|
||||||
|
activeEntrypointFiles.add(hashedName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const fileName of readdirSync(buildDir)) {
|
||||||
|
if (
|
||||||
|
/^main\.dart\.[0-9a-f]{12}\.(?:js|mjs|wasm)(?:\.br)?$/.test(fileName) &&
|
||||||
|
!activeEntrypointFiles.has(fileName.replace(/\.br$/, ''))
|
||||||
|
) {
|
||||||
|
unlinkSync(join(buildDir, fileName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bootstrap = bootstrap.replace(
|
||||||
|
/_flutter\.loader\.load\(\{\s*serviceWorkerSettings:\s*\{[\s\S]*?\}\s*\}\);/,
|
||||||
|
'_flutter.loader.load({config:{canvasKitBaseUrl:"canvaskit/"}});',
|
||||||
|
);
|
||||||
|
bootstrap = bootstrap.replace(
|
||||||
|
/_flutter\.loader\.load\(\);/,
|
||||||
|
'_flutter.loader.load({config:{canvasKitBaseUrl:"canvaskit/"}});',
|
||||||
|
);
|
||||||
|
bootstrap = bootstrap.replace(
|
||||||
|
/_flutter\.loader\.load\(\{config:\{[^}]*\}\}\);/,
|
||||||
|
'_flutter.loader.load({config:{canvasKitBaseUrl:"canvaskit/"}});',
|
||||||
|
);
|
||||||
writeFileSync(bootstrapPath, bootstrap);
|
writeFileSync(bootstrapPath, bootstrap);
|
||||||
|
|
||||||
|
if (existsSync(indexPath)) {
|
||||||
|
let index = readFileSync(indexPath, 'utf8');
|
||||||
|
const preloadLinks = [
|
||||||
|
'<link rel="preload" href="flutter_bootstrap.js" as="script" />',
|
||||||
|
hashedEntrypoints.has('main.dart.mjs')
|
||||||
|
? `<link rel="modulepreload" href="${hashedEntrypoints.get('main.dart.mjs')}" />`
|
||||||
|
: '',
|
||||||
|
hashedEntrypoints.has('main.dart.wasm')
|
||||||
|
? `<link rel="preload" href="${hashedEntrypoints.get('main.dart.wasm')}" as="fetch" type="application/wasm" crossorigin />`
|
||||||
|
: '',
|
||||||
|
'<link rel="modulepreload" href="canvaskit/skwasm.js" />',
|
||||||
|
'<link rel="preload" href="canvaskit/skwasm.wasm" as="fetch" type="application/wasm" crossorigin />',
|
||||||
|
'<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />',
|
||||||
|
...loginFontFallbackPreloads.map(
|
||||||
|
(href) =>
|
||||||
|
`<link rel="preload" href="${href}" as="fetch" type="font/woff2" crossorigin />`,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join('\n ');
|
||||||
|
|
||||||
|
index = index
|
||||||
|
.replace(/\n\s*<link rel="preload" href="flutter_bootstrap\.js" as="script" \/>/g, '')
|
||||||
|
.replace(/\n\s*<link rel="modulepreload" href="main\.dart\.[^"]+\.mjs" \/>/g, '')
|
||||||
|
.replace(
|
||||||
|
/\n\s*<link rel="preload" href="main\.dart\.[^"]+\.wasm" as="fetch" type="application\/wasm" crossorigin \/>/g,
|
||||||
|
'',
|
||||||
|
)
|
||||||
|
.replace(/\n\s*<link rel="modulepreload" href="canvaskit\/skwasm\.js" \/>/g, '')
|
||||||
|
.replace(
|
||||||
|
/\n\s*<link rel="preload" href="canvaskit\/skwasm\.wasm" as="fetch" type="application\/wasm" crossorigin \/>/g,
|
||||||
|
'',
|
||||||
|
)
|
||||||
|
.replace(/\n\s*<link rel="preconnect" href="https:\/\/fonts\.gstatic\.com" crossorigin \/>/g, '')
|
||||||
|
.replace(
|
||||||
|
/\n\s*<link rel="preload" href="https:\/\/fonts\.gstatic\.com\/s\/(?:roboto|notosanskr)\/[^"]+\.woff2" as="fetch" type="font\/woff2" crossorigin \/>/g,
|
||||||
|
'',
|
||||||
|
);
|
||||||
|
index = index.replace('</head>', ` ${preloadLinks}\n </head>`);
|
||||||
|
writeFileSync(indexPath, index);
|
||||||
|
}
|
||||||
|
|
||||||
for (const filePath of walk(buildDir)) {
|
for (const filePath of walk(buildDir)) {
|
||||||
if (filePath.endsWith('.br')) {
|
if (filePath.endsWith('.br')) {
|
||||||
continue;
|
continue;
|
||||||
@@ -78,4 +172,16 @@ function* walk(directory) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findEntrypointSource(entrypoint, bootstrap) {
|
||||||
|
if (existsSync(join(buildDir, entrypoint))) {
|
||||||
|
return entrypoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
const extension = extname(entrypoint).replace('.', '');
|
||||||
|
const match = bootstrap.match(
|
||||||
|
new RegExp(`main\\.dart\\.[0-9a-f]{12}\\.${extension}`),
|
||||||
|
);
|
||||||
|
return match?.[0] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`[userfront] optimized ${basename(buildDir)} with hashed entrypoints and brotli assets`);
|
console.log(`[userfront] optimized ${basename(buildDir)} with hashed entrypoints and brotli assets`);
|
||||||
|
|||||||
Reference in New Issue
Block a user