Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(astro): code refactor #11343

Merged
merged 13 commits into from
Jul 17, 2024
11 changes: 6 additions & 5 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/errors/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
let highlighterLang = err.loc?.file?.split('.').pop();
if (ALTERNATIVE_JS_EXTS.includes(highlighterLang ?? '')) {
highlighterLang = 'js';
}
if (ALTERNATIVE_MD_EXTS.includes(highlighterLang ?? '')) {
} else if (ALTERNATIVE_MD_EXTS.includes(highlighterLang ?? '')) {
highlighterLang = 'md';
}
const highlightedCode = err.fullCode
Expand Down
14 changes: 7 additions & 7 deletions packages/astro/src/core/errors/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ class ErrorOverlay extends HTMLElement {

const el = this.root.querySelector(selector);

if (!el) {
return;
}

if (html) {
// Automatically detect links
text = text
Expand All @@ -706,14 +710,10 @@ class ErrorOverlay extends HTMLElement {
return `<a target="_blank" href="${v}">${v}</a>`;
})
.join(' ');
}

if (el) {
if (!html) {
el.textContent = text.trim();
} else {
el.innerHTML = text.trim();
}
el.innerHTML = text.trim();
} else {
el.textContent = text.trim();
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/astro/src/core/middleware/callMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ export async function callMiddleware(
let responseFunctionPromise: Promise<Response> | 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
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/redirects/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/jsx-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/runtime/client/dev-toolbar/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading