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

Fix prerendered page handling on Deno #6284

Merged
merged 8 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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/shaggy-moons-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/deno': patch
---

Fix prerendered page behavior
2 changes: 1 addition & 1 deletion examples/deno/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"preview": "deno run --allow-net --allow-read ./dist/server/entry.mjs",
"preview": "deno run --allow-net --allow-read --allow-env ./dist/server/entry.mjs",
"astro": "astro"
},
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions examples/deno/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
import Layout from '../components/Layout.astro';

export const prerender = true;
---

<Layout title="Welcome to Astro (on Deno).">
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/deno/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"exports": {
".": "./dist/index.js",
"./server.js": "./dist/server.js",
"./__deno_imports.js": "./dist/__deno_imports.js",
"./package.json": "./package.json"
},
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions packages/integrations/deno/src/__deno_imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is a shim for any Deno-specific imports!
// It will be replaced in the final Deno build.
//
// This allows us to prerender pages in Node.
export class Server {
listenAndServe() {}
}

export function serveFile() {}
24 changes: 23 additions & 1 deletion packages/integrations/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const SHIM = `globalThis.process = {
env: Deno.env.toObject(),
};`;

// We shim deno-specific imports so we can run the code in Node
// to prerender pages. In the final Deno build, this import is
// replaced with the Deno-specific contents listed below.
const DENO_IMPORTS_SHIM = `@astrojs/deno/__deno_imports.js`;
const DENO_IMPORTS = `export { Server } from "https://deno.land/[email protected]/http/server.ts"
export { serveFile } from 'https://deno.land/[email protected]/http/file_server.ts';`

export function getAdapter(args?: Options): AstroAdapter {
return {
name: '@astrojs/deno',
Expand All @@ -29,6 +36,18 @@ export function getAdapter(args?: Options): AstroAdapter {
};
}

const denoImportsShimPlugin = {
name: '@astrojs/deno:shim',
setup(build: esbuild.PluginBuild) {
build.onLoad({ filter: /__deno_imports\.js$/ }, async (args) => {
return {
contents: DENO_IMPORTS,
loader: 'js',
}
})
},
}

export default function createIntegration(args?: Options): AstroIntegration {
let _buildConfig: BuildConfig;
let _vite: any;
Expand Down Expand Up @@ -61,10 +80,10 @@ export default function createIntegration(args?: Options): AstroIntegration {
(vite.resolve.alias as Record<string, string>)[alias.find] = alias.replacement;
}
}

vite.ssr = {
noExternal: true,
};
vite.build.rollupOptions.external = [DENO_IMPORTS_SHIM]
}
},
'astro:build:done': async () => {
Expand All @@ -80,6 +99,9 @@ export default function createIntegration(args?: Options): AstroIntegration {
format: 'esm',
bundle: true,
external: ['@astrojs/markdown-remark'],
plugins: [
denoImportsShimPlugin
],
banner: {
js: SHIM,
},
Expand Down
13 changes: 9 additions & 4 deletions packages/integrations/deno/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import type { SSRManifest } from 'astro';
import { App } from 'astro/app';

// @ts-ignore
import { Server } from 'https://deno.land/[email protected]/http/server.ts';
// @ts-ignore
import { fetch } from 'https://deno.land/x/file_fetch/mod.ts';
import { Server, serveFile } from '@astrojs/deno/__deno_imports.js';

interface Options {
port?: number;
Expand Down Expand Up @@ -40,7 +38,14 @@ export function start(manifest: SSRManifest, options: Options) {
// try to fetch a static file instead
const url = new URL(request.url);
const localPath = new URL('./' + app.removeBase(url.pathname), clientRoot);
const fileResp = await fetch(localPath.toString());
let fileResp = await serveFile(request, localPath.pathname);

// Attempt to serve `index.html` if 404
if (fileResp.status == 404) {
const fallback = new URL('./index.html', localPath).pathname;
fileResp = await serveFile(request, fallback);
}


// If the static file can't be found
if (fileResp.status == 404) {
Expand Down
20 changes: 20 additions & 0 deletions packages/integrations/deno/test/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ Deno.test({
sanitizeResources: false,
sanitizeOps: false,
});

Deno.test({
name: 'perendering',
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const resp = await fetch(new URL('perender', baseUrl));
assertEquals(resp.status, 200);

const html = await resp.text();
assert(html);

const doc = new DOMParser().parseFromString(html, `text/html`);
const h1 = doc!.querySelector('h1');
assertEquals(h1!.innerText, 'test');
});
},
sanitizeResources: false,
sanitizeOps: false,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
export const prerender = true;
---

<html>
<body>
<h1>test</h1>
</body>
</html>