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

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

Merged
merged 6 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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/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
11 changes: 8 additions & 3 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { isServerLikeOutput } from '../../prerender/utils.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { debug, info } from '../logger/core.js';
import { getRedirectLocationOrThrow, RedirectSinglePageBuiltModule } from '../redirects/index.js';
import { RedirectSinglePageBuiltModule, getRedirectLocationOrThrow } from '../redirects/index.js';
import { isEndpointResult } from '../render/core.js';
import { createEnvironment, createRenderContext, tryRenderRoute } from '../render/index.js';
import { callGetStaticPaths } from '../render/route-cache.js';
Expand Down 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;
ematipico marked this conversation as resolved.
Show resolved Hide resolved
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} />
36 changes: 36 additions & 0 deletions packages/integrations/node/test/image.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from 'chai';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';

describe('Image endpoint zzz', () => {
/** @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'
);
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.