Skip to content

Commit

Permalink
refactor: fix test regressions!!
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 16, 2023
1 parent 1095ce6 commit 410a18a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
30 changes: 21 additions & 9 deletions packages/astro/src/core/build/buildPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class BuildPipeline extends Pipeline {
#internals: BuildInternals;
#staticBuildOptions: StaticBuildOptions;
#manifest: SSRManifest;
#currentEndpointBody?: {
body: string | Uint8Array;
encoding: BufferEncoding;
};

constructor(
staticBuildOptions: StaticBuildOptions,
Expand Down Expand Up @@ -92,18 +96,24 @@ export class BuildPipeline extends Pipeline {
*
* @param staticBuildOptions
*/
static async retrieveManifest(staticBuildOptions: StaticBuildOptions): Promise<SSRManifest> {
static async retrieveManifest(
staticBuildOptions: StaticBuildOptions,
internals: BuildInternals
): Promise<SSRManifest> {
const config = staticBuildOptions.settings.config;
const baseDirectory = getOutputDirectory(config);
const manifestEntryUrl = new URL('manifest.mjs', baseDirectory);
const { default: manifest } = await import(manifestEntryUrl.toString());
const manifestEntryUrl = new URL(
`${internals.manifestFileName}?time=${Date.now()}`,
baseDirectory
);
const { manifest } = await import(manifestEntryUrl.toString());
if (!manifest) {
throw new Error(
"Astro couldn't find the emitted manifest. This is an internal error, please file an issue."
);
}

const renderersEntryUrl = new URL('renderers.mjs', baseDirectory);
const renderersEntryUrl = new URL(`renderers.mjs?time=${Date.now()}`, baseDirectory);
const renderers = await import(renderersEntryUrl.toString());
if (!renderers) {
throw new Error(
Expand Down Expand Up @@ -153,7 +163,7 @@ export class BuildPipeline extends Pipeline {
return pages;
}

async #handleEndpointResult(_: Request, response: EndpointCallResult): Promise<Response> {
async #handleEndpointResult(request: Request, response: EndpointCallResult): Promise<Response> {
if (response.type === 'response') {
if (!response.response.body) {
return new Response(null);
Expand All @@ -162,6 +172,10 @@ export class BuildPipeline extends Pipeline {
return response.response;
} else {
if (response.encoding) {
this.#currentEndpointBody = {
body: response.body,
encoding: response.encoding,
};
const headers = new Headers();
headers.set('X-Astro-Encoding', response.encoding);
return new Response(response.body, {
Expand All @@ -181,10 +195,8 @@ export class BuildPipeline extends Pipeline {
encoding: BufferEncoding;
}> {
const encoding = response.headers.get('X-Astro-Encoding') ?? 'utf-8';
if (routeType === 'endpoint') {
const ab = await response.arrayBuffer();
const body = new Uint8Array(ab);
return { body, encoding: encoding as BufferEncoding };
if (this.#currentEndpointBody) {
return this.#currentEndpointBody;
} else {
return { body: await response.text(), encoding: encoding as BufferEncoding };
}
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
const ssr = isServerLikeOutput(opts.settings.config);
let manifest: SSRManifest;
if (ssr) {
manifest = await BuildPipeline.retrieveManifest(opts);
manifest = await BuildPipeline.retrieveManifest(opts, internals);
} else {
const baseDirectory = getOutputDirectory(opts.settings.config);
const renderersEntryUrl = new URL('renderers.mjs', baseDirectory);
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface BuildInternals {
ssrEntryChunk?: Rollup.OutputChunk;
// The SSR manifest entry chunk.
manifestEntryChunk?: Rollup.OutputChunk;
manifestFileName?: string;
entryPoints: Map<RouteData, URL>;
ssrSplitEntryChunks: Map<string, Rollup.OutputChunk>;
componentMetadata: SSRResult['componentMetadata'];
Expand Down
10 changes: 9 additions & 1 deletion packages/astro/src/core/build/plugins/plugin-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ function vitePluginManifest(options: StaticBuildOptions, internals: BuildInterna
return RESOLVED_SSR_MANIFEST_VIRTUAL_MODULE_ID;
}
},
augmentChunkHash(chunkInfo) {
if (chunkInfo.facadeModuleId === RESOLVED_SSR_MANIFEST_VIRTUAL_MODULE_ID) {
return Date.now().toString();
}
},
async load(id) {
if (id === RESOLVED_SSR_MANIFEST_VIRTUAL_MODULE_ID) {
const imports = [];
Expand All @@ -46,7 +51,7 @@ const manifest = _deserializeManifest('${manifestReplace}');
_privateSetManifestDontUseThis(manifest);
`);

exports.push('export default manifest');
exports.push('export { manifest }');

return `${imports.join('\n')}${contents.join('\n')}${exports.join('\n')}`;
}
Expand All @@ -61,6 +66,9 @@ _privateSetManifestDontUseThis(manifest);
internals.manifestEntryChunk = chunk;
delete bundle[chunkName];
}
if (chunkName.startsWith('manifest')) {
internals.manifestFileName = chunkName;
}
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function generateSSRCode(config: AstroConfig, adapter: AstroAdapter) {

contents.push(`import * as adapter from '${adapter.serverEntrypoint}';
import { renderers } from '${RENDERERS_MODULE_ID}';
import defaultManifest from '${SSR_MANIFEST_VIRTUAL_MODULE_ID}';
import { manifest as defaultManifest} from '${SSR_MANIFEST_VIRTUAL_MODULE_ID}';
const _manifest = Object.assign(defaultManifest, {
${pageMap},
renderers,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async function ssrBuild(
} else if (chunkInfo.facadeModuleId === RESOLVED_RENDERERS_MODULE_ID) {
return 'renderers.mjs';
} else if (chunkInfo.facadeModuleId === RESOLVED_SSR_MANIFEST_VIRTUAL_MODULE_ID) {
return 'manifest.mjs';
return 'manifest.[hash].mjs';
} else {
return '[name].mjs';
}
Expand Down

0 comments on commit 410a18a

Please sign in to comment.