test: add test in the Node adapter for astro:assets (#7734)

This commit is contained in:
Erika 2023-07-26 23:58:47 +02:00 committed by GitHub
parent 31c4031ba7
commit d5f526b339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 82 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix some global state related to `astro:assets` not getting cleaned out properly in SSR with no pre-rendered pages

View file

@ -130,7 +130,12 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
? opts.settings.config.build.server ? opts.settings.config.build.server
: getOutDirWithinCwd(opts.settings.config.outDir); : getOutDirWithinCwd(opts.settings.config.outDir);
if (ssr && !hasPrerenderedPages(internals)) return; // HACK! `astro:assets` relies on a global to know if its running in dev, prod, ssr, ssg, full moon
// If we don't delete it here, it's technically not impossible (albeit improbable) for it to leak
if (ssr && !hasPrerenderedPages(internals)) {
delete globalThis?.astroAsset?.addStaticImage;
return;
}
const verb = ssr ? 'prerendering' : 'generating'; const verb = ssr ? 'prerendering' : 'generating';
info(opts.logging, null, `\n${bgGreen(black(` ${verb} static routes `))}`); info(opts.logging, null, `\n${bgGreen(black(` ${verb} static routes `))}`);
@ -186,7 +191,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
await generateImage(opts, imageData[1].options, imageData[1].path); await generateImage(opts, imageData[1].options, imageData[1].path);
} }
delete globalThis.astroAsset.addStaticImage; delete globalThis?.astroAsset?.addStaticImage;
} }
await runHookBuildGenerated({ await runHookBuildGenerated({

View file

@ -132,14 +132,14 @@ describe('Assets Prefix - Server', () => {
expect(island.attr('renderer-url')).to.match(assetsPrefixRegex); expect(island.attr('renderer-url')).to.match(assetsPrefixRegex);
}); });
it('markdown image src start with assetsPrefix', async () => { it('markdown optimized image src does not start with assetsPrefix in SSR', async () => {
const request = new Request('http://example.com/custom-base/markdown/'); const request = new Request('http://example.com/custom-base/markdown/');
const response = await app.render(request); const response = await app.render(request);
expect(response.status).to.equal(200); expect(response.status).to.equal(200);
const html = await response.text(); const html = await response.text();
const $ = cheerio.load(html); const $ = cheerio.load(html);
const imgAsset = $('img'); const imgAsset = $('img');
expect(imgAsset.attr('src')).to.match(assetsPrefixRegex); expect(imgAsset.attr('src')).to.not.match(assetsPrefixRegex);
}); });
}); });

View file

@ -0,0 +1,13 @@
{
"name": "@test/nodejs-image",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
},
"scripts": {
"build": "astro build",
"preview": "astro preview"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 KiB

View file

@ -0,0 +1,6 @@
---
import { Image } from "astro:assets";
import penguin from "../assets/some_penguin.png";
---
<Image src={penguin} alt="Penguins" width={50} />

View file

@ -0,0 +1,40 @@
import { expect } from 'chai';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
describe('Image endpoint', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devPreview;
before(async () => {
fixture = await loadFixture({
root: './fixtures/image/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
experimental: {
assets: true,
},
});
await fixture.build();
devPreview = await fixture.preview();
});
after(async () => {
await devPreview.stop();
});
it('it returns images', async () => {
const res = await fixture.fetch('/');
expect(res.status).to.equal(200);
const resImage = await fixture.fetch(
'/_image?href=/_astro/some_penguin.97ef5f92.png&w=50&f=webp'
);
console.log(resImage);
const content = resImage.text();
console.log(content);
expect(resImage.status).to.equal(200);
});
});

View file

@ -4620,6 +4620,15 @@ importers:
specifier: workspace:* specifier: workspace:*
version: link:../../../../../astro version: link:../../../../../astro
packages/integrations/node/test/fixtures/image:
dependencies:
'@astrojs/node':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/node/test/fixtures/locals: packages/integrations/node/test/fixtures/locals:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':