Skip to content

Commit

Permalink
fix ssr url search params bug (withastro#3066)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Apr 11, 2022
1 parent 19a4c3f commit 0f0dd8d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
5 changes: 4 additions & 1 deletion src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
91 changes: 62 additions & 29 deletions test/astro-global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.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.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.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');
});
});
});
1 change: 1 addition & 0 deletions test/fixtures/astro-global/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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 />
Expand Down

0 comments on commit 0f0dd8d

Please sign in to comment.