0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix ssr url search params bug (#3066)

This commit is contained in:
Fred K. Schott 2022-04-11 15:57:18 -07:00 committed by GitHub
parent e606dded87
commit 5b3464a803
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 33 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix bug with inconsistent url search params

View file

@ -618,13 +618,13 @@ export interface AstroUserConfig {
experimental?: {
/**
* Enable experimental support for 3rd-party integrations.
* Enable support for 3rd-party integrations.
* Default: false
*/
integrations?: boolean;
/**
* Enable a build for SSR support.
* Enable support for 3rd-party SSR adapters.
* Default: false
*/
ssr?: boolean;

View file

@ -49,7 +49,6 @@ function printAstroHelp() {
['--config <path>', 'Specify the path to the Astro config file.'],
['--root <path>', 'Specify the path to the project root folder.'],
['--legacy-build', 'Use the build strategy prior to 0.24.0'],
['--experimental-ssr', 'Enable SSR compilation fot 3rd-party adapters.'],
['--drafts', 'Include markdown draft pages in the build.'],
['--verbose', 'Enable verbose logging'],
['--silent', 'Disable logging'],

View file

@ -170,7 +170,10 @@ async function handleRequest(
const rootRelativeUrl = pathname.substring(devRoot.length - 1);
if (!buildingToSSR) {
// Prevent user from depending on search params when not doing SSR.
for (const [key] of url.searchParams) {
// NOTE: Create an array copy here because deleting-while-iterating
// creates bugs where not all search params are removed.
const allSearchParams = Array.from(url.searchParams);
for (const [key] of allSearchParams) {
url.searchParams.delete(key);
}
}

View file

@ -9,44 +9,77 @@ describe('Astro.*', () => {
fixture = await loadFixture({
root: './fixtures/astro-global/',
});
await fixture.build();
});
it('Astro.request.url', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
describe('dev', () => {
let devServer;
let $;
expect($('#pathname').text()).to.equal('/');
expect($('#child-pathname').text()).to.equal('/');
expect($('#nested-child-pathname').text()).to.equal('/');
before(async () => {
devServer = await fixture.startDevServer();
const html = await fixture.fetch('/blog/?foo=42').then((res) => res.text());
$ = cheerio.load(html);
});
after(async () => {
await devServer.stop();
});
it('Astro.request.url', async () => {
expect($('#pathname').text()).to.equal('/blog/');
expect($('#searchparams').text()).to.equal('{}');
expect($('#child-pathname').text()).to.equal('/blog/');
expect($('#nested-child-pathname').text()).to.equal('/blog/');
});
});
it('Astro.canonicalURL', async () => {
// given a URL, expect the following canonical URL
const canonicalURLs = {
'/index.html': 'https://mysite.dev/blog/',
'/post/post/index.html': 'https://mysite.dev/blog/post/post/',
'/posts/1/index.html': 'https://mysite.dev/blog/posts/',
'/posts/2/index.html': 'https://mysite.dev/blog/posts/2/',
};
for (const [url, canonicalURL] of Object.entries(canonicalURLs)) {
const html = await fixture.readFile(url);
describe('build', () => {
before(async () => {
await fixture.build();
});
// BUG: Doesn't seem like `base` config is being respected in build,
// most values are incorrect actual, does not match expected.
it.skip('Astro.request.url', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('link[rel="canonical"]').attr('href')).to.equal(canonicalURL);
}
});
it('Astro.site', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#site').attr('href')).to.equal('https://mysite.dev/blog/');
});
expect($('#pathname').text()).to.equal('/blog/');
expect($('#searchparams').text()).to.equal('{}');
expect($('#child-pathname').text()).to.equal('/blog/');
expect($('#nested-child-pathname').text()).to.equal('/blog/');
});
it('Astro.glob() correctly returns an array of all posts', async () => {
const html = await fixture.readFile('/posts/1/index.html');
const $ = cheerio.load(html);
expect($('.post-url').attr('href')).to.equal('/blog/post/post-2');
it('Astro.canonicalURL', async () => {
// given a URL, expect the following canonical URL
const canonicalURLs = {
'/index.html': 'https://mysite.dev/blog/',
'/post/post/index.html': 'https://mysite.dev/blog/post/post/',
'/posts/1/index.html': 'https://mysite.dev/blog/posts/',
'/posts/2/index.html': 'https://mysite.dev/blog/posts/2/',
};
for (const [url, canonicalURL] of Object.entries(canonicalURLs)) {
const html = await fixture.readFile(url);
const $ = cheerio.load(html);
expect($('link[rel="canonical"]').attr('href')).to.equal(canonicalURL);
}
});
it('Astro.site', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#site').attr('href')).to.equal('https://mysite.dev/blog/');
});
it('Astro.glob() correctly returns an array of all posts', async () => {
const html = await fixture.readFile('/posts/1/index.html');
const $ = cheerio.load(html);
expect($('.post-url').attr('href')).to.equal('/blog/post/post-2');
});
});
});

View file

@ -8,6 +8,7 @@ import Child from '../components/Child.astro';
</head>
<body>
<div id="pathname">{new URL(Astro.request.url).pathname}</div>
<div id="searchparams">{JSON.stringify(new URL(Astro.request.url).searchParams)}</div>
<a id="site" href={Astro.site}>Home</a>
<Child />