0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

refactor(astro): code refactor (#11343)

This commit is contained in:
Simon He 2024-07-17 21:03:45 +08:00 committed by GitHub
parent e30cf49ee4
commit 6e459f03c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 26 additions and 26 deletions

View file

@ -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)) {

View file

@ -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

View file

@ -695,6 +695,10 @@ class ErrorOverlay extends HTMLElement {
const el = this.root.querySelector(selector);
if (!el) {
return;
}
if (html) {
// Automatically detect links
text = text
@ -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();
}
}

View file

@ -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

View file

@ -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') {

View file

@ -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) => {

View file

@ -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)