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

Fix for checking media type in CSR #8537

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-beds-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

bugfix checking media-type in client-side router
24 changes: 12 additions & 12 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ const { fallback = 'animate' } = Astro.props as Props;
};

async function getHTML(href: string) {
let res;
try {
res = await fetch(href);
const res = await fetch(href);
const html = await res.text();
return {
ok: res.ok,
html,
redirected: res.redirected ? res.url : undefined,
// drop potential charset (+ other name/value pairs) as parser needs the mediaType
mediaType: res.headers.get('content-type')?.replace(/;.*/, ''),
martrapp marked this conversation as resolved.
Show resolved Hide resolved
};
} catch (err) {
return { ok: false };
}
const html = await res.text();
return {
ok: res.ok,
html,
redirected: res.redirected ? res.url : undefined,
contentType: res.headers.get('content-type'),
};
}

function getFallback(): Fallback {
Expand Down Expand Up @@ -290,16 +290,16 @@ const { fallback = 'animate' } = Astro.props as Props;
async function navigate(dir: Direction, loc: URL, state?: State) {
let finished: Promise<void>;
const href = loc.href;
const { html, ok, contentType, redirected } = await getHTML(href);
const { html, ok, mediaType, redirected } = await getHTML(href);
// if there was a redirection, show the final URL in the browser's address bar
redirected && (loc = new URL(redirected));
// If there is a problem fetching the new page, just do an MPA navigation to it.
if (!ok || contentType !== 'text/html') {
if (!ok || !(mediaType === 'text/html' || mediaType === 'application/xhtml+xml')) {
location.href = href;
return;
}

const doc = parser.parseFromString(html, contentType);
const doc = parser.parseFromString(html, mediaType);
if (!doc.querySelector('[name="astro-view-transitions-enabled"]')) {
location.href = href;
return;
Expand Down
Loading