From 6e459f03c69c48e97d123cf615d97e046a9ed078 Mon Sep 17 00:00:00 2001 From: Simon He <57086651+Simon-He95@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:03:45 +0800 Subject: [PATCH] refactor(astro): code refactor (#11343) --- packages/astro/src/core/build/static-build.ts | 11 ++++++----- packages/astro/src/core/errors/dev/vite.ts | 3 +-- packages/astro/src/core/errors/overlay.ts | 14 +++++++------- .../astro/src/core/middleware/callMiddleware.ts | 13 +++++++------ packages/astro/src/core/redirects/render.ts | 3 +-- packages/astro/src/jsx-runtime/index.ts | 3 +-- .../src/runtime/client/dev-toolbar/entrypoint.ts | 5 +++-- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 5b75dbc935..da651effdc 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -215,14 +215,15 @@ async function ssrBuild( if (isContentCache) { prefix += `${buildID}/`; suffix = '.mjs'; - } - if (isContentCache && name.includes('/content/')) { - const parts = name.split('/'); - if (parts.at(1) === 'content') { - return encodeName(parts.slice(1).join('/')); + if (name.includes('/content/')) { + const parts = name.split('/'); + if (parts.at(1) === 'content') { + return encodeName(parts.slice(1).join('/')); + } } } + // Sometimes chunks have the `@_@astro` suffix due to SSR logic. Remove it! // TODO: refactor our build logic to avoid this if (name.includes(ASTRO_PAGE_EXTENSION_POST_PATTERN)) { diff --git a/packages/astro/src/core/errors/dev/vite.ts b/packages/astro/src/core/errors/dev/vite.ts index 96e53d41c2..4d70a52686 100644 --- a/packages/astro/src/core/errors/dev/vite.ts +++ b/packages/astro/src/core/errors/dev/vite.ts @@ -146,8 +146,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise${v}`; }) .join(' '); - } - if (el) { - if (!html) { - el.textContent = text.trim(); - } else { - el.innerHTML = text.trim(); - } + el.innerHTML = text.trim(); + } else { + el.textContent = text.trim(); } } diff --git a/packages/astro/src/core/middleware/callMiddleware.ts b/packages/astro/src/core/middleware/callMiddleware.ts index d52ba01265..5f4fb3d0f5 100644 --- a/packages/astro/src/core/middleware/callMiddleware.ts +++ b/packages/astro/src/core/middleware/callMiddleware.ts @@ -56,15 +56,16 @@ export async function callMiddleware( let responseFunctionPromise: Promise | Response | undefined = undefined; const next: MiddlewareNext = async (payload) => { nextCalled = true; - if (!enableRerouting && payload) { - logger.warn( - 'router', - 'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.' - ); - } + if (enableRerouting) { responseFunctionPromise = responseFunction(apiContext, payload); } else { + if (payload) { + logger.warn( + 'router', + 'The rewrite API is experimental. To use this feature, add the `rewriting` flag to the `experimental` object in your Astro config.' + ); + } responseFunctionPromise = responseFunction(apiContext); } // We need to pass the APIContext pass to `callMiddleware` because it can be mutated across middleware functions diff --git a/packages/astro/src/core/redirects/render.ts b/packages/astro/src/core/redirects/render.ts index 24361fde47..379f26e3ba 100644 --- a/packages/astro/src/core/redirects/render.ts +++ b/packages/astro/src/core/redirects/render.ts @@ -25,8 +25,7 @@ function redirectRouteGenerate(renderContext: RenderContext): string { let target = redirect; for (const param of Object.keys(params)) { const paramValue = params[param]!; - target = target.replace(`[${param}]`, paramValue); - target = target.replace(`[...${param}]`, paramValue); + target = target.replace(`[${param}]`, paramValue).replace(`[...${param}]`, paramValue); } return target; } else if (typeof redirect === 'undefined') { diff --git a/packages/astro/src/jsx-runtime/index.ts b/packages/astro/src/jsx-runtime/index.ts index b82fd97dc3..45093db14e 100644 --- a/packages/astro/src/jsx-runtime/index.ts +++ b/packages/astro/src/jsx-runtime/index.ts @@ -29,8 +29,7 @@ export function transformSlots(vnode: AstroVNode) { slots[name]['$$slot'] = true; delete child.props.slot; delete vnode.props.children; - } - if (Array.isArray(vnode.props.children)) { + } else if (Array.isArray(vnode.props.children)) { // Handle many children with slot attributes vnode.props.children = vnode.props.children .map((child) => { diff --git a/packages/astro/src/runtime/client/dev-toolbar/entrypoint.ts b/packages/astro/src/runtime/client/dev-toolbar/entrypoint.ts index 2558a4db23..48cdac72f7 100644 --- a/packages/astro/src/runtime/client/dev-toolbar/entrypoint.ts +++ b/packages/astro/src/runtime/client/dev-toolbar/entrypoint.ts @@ -92,8 +92,9 @@ document.addEventListener('DOMContentLoaded', async () => { if (!(evt instanceof CustomEvent)) return; const target = overlay.shadowRoot?.querySelector(`[data-app-id="${app.id}"]`); - const notificationElement = target?.querySelector('.notification'); - if (!target || !notificationElement) return; + if (!target) return; + const notificationElement = target.querySelector('.notification'); + if (!notificationElement) return; let newState = evt.detail.state ?? true; let level = notificationLevels.includes(evt?.detail?.level)