첫 커밋: 로컬 프로젝트 업로드
This commit is contained in:
197
baron-sso/userfront/scripts/dev-server.sh
Normal file
197
baron-sso/userfront/scripts/dev-server.sh
Normal file
@@ -0,0 +1,197 @@
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
|
||||
cd /workspace
|
||||
/bin/sh ./scripts/sync_userfront_locales.sh
|
||||
|
||||
cd /workspace/userfront
|
||||
USERFRONT_INTERNAL_PORT="${USERFRONT_INTERNAL_PORT:-5000}"
|
||||
USERFRONT_FLUTTER_RUN_FLAGS="${USERFRONT_FLUTTER_RUN_FLAGS:---debug}"
|
||||
USERFRONT_BOOT_WARMUP_ATTEMPTS="${USERFRONT_BOOT_WARMUP_ATTEMPTS:-120}"
|
||||
USERFRONT_BOOT_WARMUP_INTERVAL_SECONDS="${USERFRONT_BOOT_WARMUP_INTERVAL_SECONDS:-0.5}"
|
||||
USERFRONT_BOOT_WARMUP_LOCALES="${USERFRONT_BOOT_WARMUP_LOCALES:-ko en}"
|
||||
USERFRONT_BOOT_WARMUP_VIEWPORTS="${USERFRONT_BOOT_WARMUP_VIEWPORTS:-mobile:390 desktop:1440}"
|
||||
USERFRONT_BOOT_BUILD_MARKER="${USERFRONT_BOOT_BUILD_MARKER:-build/.userfront-boot-build-start}"
|
||||
USERFRONT_BOOT_LOG="${USERFRONT_BOOT_LOG:-build/userfront-boot-flutter.log}"
|
||||
USERFRONT_BOOT_LOG_PIPE="${USERFRONT_BOOT_LOG_PIPE:-build/userfront-boot-flutter.pipe}"
|
||||
|
||||
echo "[userfront-boot] removing stale build/web before flutter run" >&2
|
||||
rm -rf build/web
|
||||
mkdir -p "$(dirname "$USERFRONT_BOOT_BUILD_MARKER")"
|
||||
: > "$USERFRONT_BOOT_BUILD_MARKER"
|
||||
|
||||
warm_get() {
|
||||
path="$1"
|
||||
locale="$2"
|
||||
viewport="$3"
|
||||
width="${viewport#*:}"
|
||||
if [ "$width" = "$viewport" ]; then
|
||||
width=""
|
||||
fi
|
||||
|
||||
wget -qO- \
|
||||
--header="Accept-Language: $locale" \
|
||||
--header="Viewport-Width: $width" \
|
||||
"http://127.0.0.1:${USERFRONT_INTERNAL_PORT}${path}" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
wait_for_userfront_build() {
|
||||
flutter_pid="$1"
|
||||
attempt=1
|
||||
|
||||
while [ "$attempt" -le "$USERFRONT_BOOT_WARMUP_ATTEMPTS" ]; do
|
||||
if [ -f "build/web/index.html" ] && [ "build/web/index.html" -nt "$USERFRONT_BOOT_BUILD_MARKER" ]; then
|
||||
return 0
|
||||
fi
|
||||
if ! kill -0 "$flutter_pid" 2>/dev/null; then
|
||||
echo "[userfront-boot] warmup skipped because flutter exited before build/web/index.html was ready" >&2
|
||||
return 1
|
||||
fi
|
||||
attempt=$((attempt + 1))
|
||||
sleep "$USERFRONT_BOOT_WARMUP_INTERVAL_SECONDS"
|
||||
done
|
||||
|
||||
echo "[userfront-boot] warmup skipped after ${USERFRONT_BOOT_WARMUP_ATTEMPTS} build readiness attempts" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
wait_for_userfront_serve_log() {
|
||||
flutter_pid="$1"
|
||||
attempt=1
|
||||
|
||||
while [ "$attempt" -le "$USERFRONT_BOOT_WARMUP_ATTEMPTS" ]; do
|
||||
if [ -f "$USERFRONT_BOOT_LOG" ] && grep -Fq "lib/main.dart is being served at" "$USERFRONT_BOOT_LOG"; then
|
||||
return 0
|
||||
fi
|
||||
if ! kill -0 "$flutter_pid" 2>/dev/null; then
|
||||
echo "[userfront-boot] warmup skipped because flutter exited before serve log was ready" >&2
|
||||
return 1
|
||||
fi
|
||||
attempt=$((attempt + 1))
|
||||
sleep "$USERFRONT_BOOT_WARMUP_INTERVAL_SECONDS"
|
||||
done
|
||||
|
||||
echo "[userfront-boot] warmup skipped after ${USERFRONT_BOOT_WARMUP_ATTEMPTS} serve readiness attempts" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
disable_userfront_dev_service_worker() {
|
||||
cat > build/web/flutter_service_worker.js <<'EOF'
|
||||
self.addEventListener("install", (event) => {
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(self.registration.unregister());
|
||||
});
|
||||
EOF
|
||||
}
|
||||
|
||||
warm_flutter_bootstrap_assets() {
|
||||
if [ ! -f build/web/flutter_bootstrap.js ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
grep -Eo 'main\.dart\.[^"]+\.(js|mjs|wasm)' build/web/flutter_bootstrap.js \
|
||||
| sort -u \
|
||||
| while IFS= read -r asset; do
|
||||
[ -n "$asset" ] || continue
|
||||
wget -qO- "http://127.0.0.1:${USERFRONT_INTERNAL_PORT}/${asset}" >/dev/null 2>&1 || \
|
||||
echo "[userfront-boot] warmup warning: failed to load /${asset}" >&2
|
||||
done
|
||||
}
|
||||
|
||||
warm_userfront_once() {
|
||||
flutter_pid="$1"
|
||||
attempt=1
|
||||
|
||||
if ! wait_for_userfront_serve_log "$flutter_pid"; then
|
||||
return 0
|
||||
fi
|
||||
if ! wait_for_userfront_build "$flutter_pid"; then
|
||||
return 0
|
||||
fi
|
||||
disable_userfront_dev_service_worker
|
||||
|
||||
while [ "$attempt" -le "$USERFRONT_BOOT_WARMUP_ATTEMPTS" ]; do
|
||||
if wget -qO- "http://127.0.0.1:${USERFRONT_INTERNAL_PORT}/flutter_bootstrap.js" >/dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
if ! kill -0 "$flutter_pid" 2>/dev/null; then
|
||||
echo "[userfront-boot] warmup skipped because flutter exited before readiness" >&2
|
||||
return 0
|
||||
fi
|
||||
attempt=$((attempt + 1))
|
||||
sleep "$USERFRONT_BOOT_WARMUP_INTERVAL_SECONDS"
|
||||
done
|
||||
|
||||
if [ "$attempt" -gt "$USERFRONT_BOOT_WARMUP_ATTEMPTS" ]; then
|
||||
echo "[userfront-boot] warmup skipped after ${USERFRONT_BOOT_WARMUP_ATTEMPTS} readiness attempts" >&2
|
||||
return 0
|
||||
fi
|
||||
|
||||
started_at="$(date +%s)"
|
||||
echo "[userfront-boot] one-shot warmup starting locales=\"${USERFRONT_BOOT_WARMUP_LOCALES}\" viewports=\"${USERFRONT_BOOT_WARMUP_VIEWPORTS}\"" >&2
|
||||
|
||||
for locale in $USERFRONT_BOOT_WARMUP_LOCALES; do
|
||||
for viewport in $USERFRONT_BOOT_WARMUP_VIEWPORTS; do
|
||||
warm_get "/${locale}/signin" "$locale" "$viewport" || true
|
||||
done
|
||||
done
|
||||
|
||||
for asset in \
|
||||
/ \
|
||||
/flutter_bootstrap.js \
|
||||
/canvaskit/skwasm.js \
|
||||
/canvaskit/skwasm.wasm \
|
||||
/canvaskit/skwasm_heavy.js \
|
||||
/canvaskit/skwasm_heavy.wasm \
|
||||
/assets/AssetManifest.bin.json \
|
||||
/assets/FontManifest.json
|
||||
do
|
||||
wget -qO- "http://127.0.0.1:${USERFRONT_INTERNAL_PORT}${asset}" >/dev/null 2>&1 || true
|
||||
done
|
||||
warm_flutter_bootstrap_assets
|
||||
|
||||
finished_at="$(date +%s)"
|
||||
elapsed_seconds=$((finished_at - started_at))
|
||||
echo "[userfront-boot] one-shot warmup completed in ${elapsed_seconds}s" >&2
|
||||
}
|
||||
|
||||
set -- flutter run \
|
||||
-d web-server \
|
||||
--web-hostname 0.0.0.0 \
|
||||
--web-port "${USERFRONT_INTERNAL_PORT}" \
|
||||
--wasm \
|
||||
--dart-define=BACKEND_URL="${BACKEND_URL:-}" \
|
||||
--dart-define=CLIENT_LOG_DEBUG="${CLIENT_LOG_DEBUG:-false}" \
|
||||
--dart-define=APP_ENV="${APP_ENV:-dev}" \
|
||||
--dart-define=USERFRONT_URL="${USERFRONT_URL:-}" \
|
||||
${USERFRONT_FLUTTER_RUN_FLAGS} \
|
||||
--no-web-resources-cdn
|
||||
|
||||
mkdir -p "$(dirname "$USERFRONT_BOOT_LOG")"
|
||||
rm -f "$USERFRONT_BOOT_LOG" "$USERFRONT_BOOT_LOG_PIPE"
|
||||
mkfifo "$USERFRONT_BOOT_LOG_PIPE"
|
||||
tee "$USERFRONT_BOOT_LOG" < "$USERFRONT_BOOT_LOG_PIPE" &
|
||||
tee_pid="$!"
|
||||
|
||||
"$@" > "$USERFRONT_BOOT_LOG_PIPE" 2>&1 &
|
||||
flutter_pid="$!"
|
||||
|
||||
terminate() {
|
||||
kill "$flutter_pid" 2>/dev/null || true
|
||||
kill "$tee_pid" 2>/dev/null || true
|
||||
wait "$flutter_pid" 2>/dev/null || true
|
||||
wait "$tee_pid" 2>/dev/null || true
|
||||
rm -f "$USERFRONT_BOOT_LOG_PIPE"
|
||||
}
|
||||
|
||||
trap terminate INT TERM
|
||||
warm_userfront_once "$flutter_pid"
|
||||
set +e
|
||||
wait "$flutter_pid"
|
||||
flutter_status="$?"
|
||||
wait "$tee_pid" 2>/dev/null || true
|
||||
rm -f "$USERFRONT_BOOT_LOG_PIPE"
|
||||
exit "$flutter_status"
|
||||
364
baron-sso/userfront/scripts/optimize-web-build.mjs
Normal file
364
baron-sso/userfront/scripts/optimize-web-build.mjs
Normal file
@@ -0,0 +1,364 @@
|
||||
import { brotliCompressSync, constants } from 'node:zlib';
|
||||
import { createHash } from 'node:crypto';
|
||||
import {
|
||||
existsSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
renameSync,
|
||||
unlinkSync,
|
||||
writeFileSync,
|
||||
} from 'node:fs';
|
||||
import { basename, extname, join, relative } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname } from 'node:path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
const buildDir = process.argv[2] ?? join(__dirname, '..', 'build', 'web');
|
||||
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 compressibleExtensions = new Set([
|
||||
'.css',
|
||||
'.html',
|
||||
'.js',
|
||||
'.json',
|
||||
'.mjs',
|
||||
'.svg',
|
||||
'.toml',
|
||||
'.wasm',
|
||||
]);
|
||||
const serviceWorkerPath = join(buildDir, 'flutter_service_worker.js');
|
||||
|
||||
if (!existsSync(bootstrapPath)) {
|
||||
throw new Error(`Missing Flutter bootstrap file: ${bootstrapPath}`);
|
||||
}
|
||||
|
||||
let bootstrap = readFileSync(bootstrapPath, 'utf8');
|
||||
const hashedEntrypoints = new Map();
|
||||
const activeEntrypointFiles = new Set();
|
||||
|
||||
for (const entrypoint of hashableEntrypoints) {
|
||||
const sourceName = findEntrypointSource(entrypoint, bootstrap);
|
||||
if (!sourceName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const sourcePath = join(buildDir, sourceName);
|
||||
if (!existsSync(sourcePath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const content = readFileSync(sourcePath);
|
||||
const hash = createHash('sha256').update(content).digest('hex').slice(0, 12);
|
||||
const extension = extname(entrypoint);
|
||||
const stem = entrypoint.slice(0, -extension.length);
|
||||
const hashedName = `${stem}.${hash}${extension}`;
|
||||
const targetPath = join(buildDir, hashedName);
|
||||
|
||||
if (sourceName !== hashedName) {
|
||||
renameSync(sourcePath, targetPath);
|
||||
}
|
||||
bootstrap = bootstrap.replaceAll(sourceName, 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));
|
||||
}
|
||||
}
|
||||
|
||||
const canvasKitConfig = 'config:{canvasKitBaseUrl:"canvaskit/"}';
|
||||
|
||||
bootstrap = bootstrap.replace(
|
||||
/_flutter\.loader\.load\(\{\s*serviceWorkerSettings:\s*(\{[^{}]*\})\s*,\s*config:\s*\{[\s\S]*?serviceWorkerUrl[\s\S]*?\}\s*,\s*config:\s*\{[^}]*\}\s*\}\);/g,
|
||||
(_match, settings) =>
|
||||
`_flutter.loader.load({serviceWorkerSettings:${ensureServiceWorkerUrl(settings)},${canvasKitConfig}});`,
|
||||
);
|
||||
bootstrap = bootstrap.replace(
|
||||
/_flutter\.loader\.load\(\{\s*serviceWorkerSettings:\s*(\{[^{}]*\})\s*,\s*config:\s*\{[^}]*\}\s*\}\);/g,
|
||||
(_match, settings) =>
|
||||
`_flutter.loader.load({serviceWorkerSettings:${ensureServiceWorkerUrl(settings)},${canvasKitConfig}});`,
|
||||
);
|
||||
bootstrap = bootstrap.replace(
|
||||
/_flutter\.loader\.load\(\{\s*serviceWorkerSettings:\s*(\{[^{}]*\})\s*\}\);/g,
|
||||
(_match, settings) =>
|
||||
`_flutter.loader.load({serviceWorkerSettings:${ensureServiceWorkerUrl(settings)},${canvasKitConfig}});`,
|
||||
);
|
||||
bootstrap = bootstrap.replace(
|
||||
/_flutter\.loader\.load\(\);/g,
|
||||
`_flutter.loader.load({${canvasKitConfig}});`,
|
||||
);
|
||||
bootstrap = bootstrap.replace(
|
||||
/_flutter\.loader\.load\(\{config:\{[^}]*\}\}\);/g,
|
||||
`_flutter.loader.load({${canvasKitConfig}});`,
|
||||
);
|
||||
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.js') &&
|
||||
!(hashedEntrypoints.has('main.dart.mjs') && hashedEntrypoints.has('main.dart.wasm'))
|
||||
? `<link rel="preload" href="${hashedEntrypoints.get('main.dart.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 />',
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join('\n ');
|
||||
|
||||
index = index
|
||||
.replace(/\n\s*<link rel="preload" href="flutter_bootstrap\.js" as="script" \/>/g, '')
|
||||
.replace(/\n\s*<link rel="preload" href="main\.dart\.[^"]+\.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="preload" href="assets\/assets\/translations\/(?:en|ko)\.toml" as="fetch" 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);
|
||||
}
|
||||
|
||||
writeFileSync(serviceWorkerPath, createServiceWorker());
|
||||
|
||||
for (const filePath of walk(buildDir)) {
|
||||
if (filePath.endsWith('.br')) {
|
||||
continue;
|
||||
}
|
||||
if (!compressibleExtensions.has(extname(filePath))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const content = readFileSync(filePath);
|
||||
writeFileSync(
|
||||
`${filePath}.br`,
|
||||
brotliCompressSync(content, {
|
||||
params: {
|
||||
[constants.BROTLI_PARAM_QUALITY]: 11,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function* walk(directory) {
|
||||
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
||||
const entryPath = join(directory, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
yield* walk(entryPath);
|
||||
continue;
|
||||
}
|
||||
if (entry.isFile()) {
|
||||
yield entryPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function createServiceWorker() {
|
||||
const assets = [];
|
||||
const versionHash = createHash('sha256');
|
||||
|
||||
for (const filePath of walk(buildDir)) {
|
||||
if (filePath.endsWith('.br') || filePath === serviceWorkerPath) {
|
||||
continue;
|
||||
}
|
||||
const extension = extname(filePath);
|
||||
if (
|
||||
!compressibleExtensions.has(extension) &&
|
||||
!['.ico', '.png', '.webp', '.woff', '.woff2'].includes(extension)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const assetPath = `/${relative(buildDir, filePath).replaceAll('\\', '/')}`;
|
||||
if (!shouldPrecacheAsset(assetPath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
assets.push(assetPath);
|
||||
versionHash.update(assetPath);
|
||||
versionHash.update(readFileSync(filePath));
|
||||
}
|
||||
|
||||
assets.sort();
|
||||
const version = versionHash.digest('hex').slice(0, 16);
|
||||
const serializedAssets = JSON.stringify(assets, null, 2);
|
||||
|
||||
return `'use strict';
|
||||
|
||||
const CACHE_NAME = 'baron-userfront-${version}';
|
||||
const CORE_ASSETS = ${serializedAssets};
|
||||
const NETWORK_FIRST_PATHS = new Set([
|
||||
'/',
|
||||
'/index.html',
|
||||
'/flutter_bootstrap.js',
|
||||
'/version.json',
|
||||
'/manifest.json',
|
||||
]);
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.open(CACHE_NAME)
|
||||
.then((cache) => cache.addAll(CORE_ASSETS))
|
||||
.then(() => self.skipWaiting()),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) =>
|
||||
Promise.all(
|
||||
keys
|
||||
.filter((key) => key !== CACHE_NAME)
|
||||
.map((key) => caches.delete(key)),
|
||||
),
|
||||
)
|
||||
.then(() => self.clients.claim()),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const request = event.request;
|
||||
if (request.method !== 'GET') return;
|
||||
|
||||
const url = new URL(request.url);
|
||||
if (url.origin !== self.location.origin || url.pathname.startsWith('/api/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isAppShellPath(url) || NETWORK_FIRST_PATHS.has(url.pathname)) {
|
||||
event.respondWith(networkFirst(request, '/index.html'));
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith(cacheFirst(request));
|
||||
});
|
||||
|
||||
function isAppShellPath(url) {
|
||||
return !url.pathname.split('/').pop().includes('.');
|
||||
}
|
||||
|
||||
async function networkFirst(request, fallbackPath) {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
if (response.ok) {
|
||||
await cache.put(request, response.clone());
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
return (
|
||||
(await cache.match(request)) ??
|
||||
(fallbackPath ? await cache.match(fallbackPath) : undefined) ??
|
||||
Response.error()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function cacheFirst(request) {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const cached = await cache.match(request);
|
||||
if (cached) return cached;
|
||||
const response = await fetch(request);
|
||||
if (response.ok) {
|
||||
await cache.put(request, response.clone());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
function shouldPrecacheAsset(assetPath) {
|
||||
if (
|
||||
[
|
||||
'/index.html',
|
||||
'/flutter_bootstrap.js',
|
||||
'/manifest.json',
|
||||
'/version.json',
|
||||
'/assets/AssetManifest.bin.json',
|
||||
'/assets/FontManifest.json',
|
||||
].includes(assetPath)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (/^\/assets\/assets\/translations\/(?:en|ko|template)\.toml$/.test(assetPath)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function ensureServiceWorkerUrl(settings) {
|
||||
const serviceWorkerUrl = `"/flutter_service_worker.js?v=" + ${serviceWorkerVersionExpression(settings)}`;
|
||||
if (/serviceWorkerUrl\s*:/.test(settings)) {
|
||||
return settings.replace(
|
||||
/serviceWorkerUrl\s*:\s*[^,\n}]+,?/,
|
||||
`serviceWorkerUrl: ${serviceWorkerUrl},`,
|
||||
);
|
||||
}
|
||||
|
||||
const closingBraceIndex = settings.lastIndexOf('}');
|
||||
if (closingBraceIndex < 0) {
|
||||
return settings;
|
||||
}
|
||||
const beforeClosing = settings.slice(0, closingBraceIndex).trimEnd();
|
||||
const afterClosing = settings.slice(closingBraceIndex);
|
||||
const separator =
|
||||
beforeClosing.endsWith('{') || beforeClosing.endsWith(',') ? '' : ',';
|
||||
return `${beforeClosing}${separator}
|
||||
serviceWorkerUrl: ${serviceWorkerUrl},
|
||||
${afterClosing}`;
|
||||
}
|
||||
|
||||
function serviceWorkerVersionExpression(settings) {
|
||||
const match = settings.match(/serviceWorkerVersion\s*:\s*([^,\n}]+)/);
|
||||
return (
|
||||
match?.[1]?.replace(/\/\*[\s\S]*?\*\//g, '').trim() ??
|
||||
'serviceWorkerVersion'
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`[userfront] optimized ${basename(buildDir)} with hashed entrypoints and brotli assets`);
|
||||
Reference in New Issue
Block a user