Skip to content

Commit

Permalink
test: add test in the Node adapter for astro:assets (#7734)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh authored Jul 26, 2023
1 parent 31c4031 commit d5f526b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-gifts-jam.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 7 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
? opts.settings.config.build.server
: 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';
info(opts.logging, null, `\n${bgGreen(black(` ${verb} static routes `))}`);
Expand Down Expand Up @@ -186,7 +191,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
await generateImage(opts, imageData[1].options, imageData[1].path);
}

delete globalThis.astroAsset.addStaticImage;
delete globalThis?.astroAsset?.addStaticImage;
}

await runHookBuildGenerated({
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/astro-assets-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ describe('Assets Prefix - Server', () => {
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 response = await app.render(request);
expect(response.status).to.equal(200);
const html = await response.text();
const $ = cheerio.load(html);
const imgAsset = $('img');
expect(imgAsset.attr('src')).to.match(assetsPrefixRegex);
expect(imgAsset.attr('src')).to.not.match(assetsPrefixRegex);
});
});

Expand Down
13 changes: 13 additions & 0 deletions packages/integrations/node/test/fixtures/image/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
import { Image } from "astro:assets";
import penguin from "../assets/some_penguin.png";
---

<Image src={penguin} alt="Penguins" width={50} />
40 changes: 40 additions & 0 deletions packages/integrations/node/test/image.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5f526b

Please sign in to comment.