Handle static file serving in Deno adapter's start command (#3121)
* Handle static file serving in Deno adapter's start command * Adds a changeset * Ignore a .ts imort
This commit is contained in:
parent
b0ba22c5ff
commit
e5383cd357
7 changed files with 55 additions and 8 deletions
5
.changeset/brave-planes-punch.md
Normal file
5
.changeset/brave-planes-punch.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/deno': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Handles file serving in the main export
|
|
@ -10,7 +10,7 @@ export function getAdapter(args?: Options): AstroAdapter {
|
||||||
name: '@astrojs/deno',
|
name: '@astrojs/deno',
|
||||||
serverEntrypoint: '@astrojs/deno/server.js',
|
serverEntrypoint: '@astrojs/deno/server.js',
|
||||||
args: args ?? {},
|
args: args ?? {},
|
||||||
exports: ['stop', 'handle'],
|
exports: ['stop', 'handle', 'start', 'running'],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ import type { SSRManifest } from 'astro';
|
||||||
import { App } from 'astro/app';
|
import { App } from 'astro/app';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { Server } from 'https://deno.land/std@0.132.0/http/server.ts';
|
import { Server } from 'https://deno.land/std@0.132.0/http/server.ts';
|
||||||
|
// @ts-ignore
|
||||||
|
import { fetch } from 'https://deno.land/x/file_fetch/mod.ts';
|
||||||
|
|
||||||
interface Options {
|
interface Options {
|
||||||
port?: number;
|
port?: number;
|
||||||
|
@ -18,32 +20,43 @@ export function start(manifest: SSRManifest, options: Options) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const clientRoot = new URL('../client/', import.meta.url);
|
||||||
const app = new App(manifest);
|
const app = new App(manifest);
|
||||||
|
|
||||||
const handler = async (request: Request) => {
|
const handler = async (request: Request) => {
|
||||||
const response = await app.render(request);
|
if(app.match(request)) {
|
||||||
return response;
|
return await app.render(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = new URL(request.url);
|
||||||
|
const localPath = new URL('.' + url.pathname, clientRoot);
|
||||||
|
return fetch(localPath.toString());
|
||||||
};
|
};
|
||||||
|
|
||||||
_server = new Server({
|
_server = new Server({
|
||||||
port: options.port ?? 8085,
|
port: options.port ?? 8085,
|
||||||
hostname: options.hostname ?? '0.0.0.0',
|
hostname: options.hostname ?? '0.0.0.0',
|
||||||
handler,
|
handler,
|
||||||
//onError: options.onError,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
_startPromise = _server.listenAndServe();
|
_startPromise = _server.listenAndServe();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createExports(manifest: SSRManifest) {
|
export function createExports(manifest: SSRManifest, options: Options) {
|
||||||
const app = new App(manifest);
|
const app = new App(manifest);
|
||||||
return {
|
return {
|
||||||
async stop() {
|
async stop() {
|
||||||
if (_server) {
|
if (_server) {
|
||||||
_server.close();
|
_server.close();
|
||||||
|
_server = undefined;
|
||||||
}
|
}
|
||||||
await Promise.resolve(_startPromise);
|
await Promise.resolve(_startPromise);
|
||||||
},
|
},
|
||||||
|
running() {
|
||||||
|
return _server !== undefined;
|
||||||
|
},
|
||||||
|
async start() {
|
||||||
|
return start(manifest, options);
|
||||||
|
},
|
||||||
async handle(request: Request) {
|
async handle(request: Request) {
|
||||||
return app.render(request);
|
return app.render(request);
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
import { runBuildAndStartApp } from './helpers.js';
|
import { runBuildAndStartApp } from './helpers.js';
|
||||||
import { assertEquals, assert } from './deps.js';
|
import { assertEquals, assert, DOMParser } from './deps.js';
|
||||||
|
|
||||||
|
async function startApp(cb) {
|
||||||
|
await runBuildAndStartApp('./fixtures/basics/', cb);
|
||||||
|
}
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: 'Basics',
|
name: 'Basics',
|
||||||
async fn() {
|
async fn() {
|
||||||
await runBuildAndStartApp('./fixtures/basics/', async () => {
|
await startApp(async () => {
|
||||||
const resp = await fetch('http://127.0.0.1:8085/');
|
const resp = await fetch('http://127.0.0.1:8085/');
|
||||||
assertEquals(resp.status, 200);
|
assertEquals(resp.status, 200);
|
||||||
const html = await resp.text();
|
const html = await resp.text();
|
||||||
|
@ -12,3 +16,23 @@ Deno.test({
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: 'Loads style assets',
|
||||||
|
async fn() {
|
||||||
|
await startApp(async () => {
|
||||||
|
let resp = await fetch('http://127.0.0.1:8085/');
|
||||||
|
const html = await resp.text();
|
||||||
|
|
||||||
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
||||||
|
const link = doc.querySelector('link');
|
||||||
|
const href = link.getAttribute('href');
|
||||||
|
|
||||||
|
resp = await fetch(new URL(href, 'http://127.0.0.1:8085/'));
|
||||||
|
assertEquals(resp.status, 200);
|
||||||
|
const ct = resp.headers.get('content-type');
|
||||||
|
assertEquals(ct, 'text/css');
|
||||||
|
await resp.body.cancel()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
export * from 'https://deno.land/std@0.110.0/path/mod.ts';
|
export * from 'https://deno.land/std@0.110.0/path/mod.ts';
|
||||||
export * from 'https://deno.land/std@0.132.0/testing/asserts.ts';
|
export * from 'https://deno.land/std@0.132.0/testing/asserts.ts';
|
||||||
|
export * from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts';
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Basic App on Deno</title>
|
<title>Basic App on Deno</title>
|
||||||
|
<style>body { font-family: 'Courier New', Courier, monospace; }</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Basic App on Deno</h1>
|
<h1>Basic App on Deno</h1>
|
||||||
|
|
|
@ -14,6 +14,9 @@ export async function runBuildAndStartApp(fixturePath, cb) {
|
||||||
const url = new URL(fixturePath, dir);
|
const url = new URL(fixturePath, dir);
|
||||||
const close = await runBuild(fixturePath);
|
const close = await runBuild(fixturePath);
|
||||||
const mod = await import(new URL('./dist/server/entry.mjs', url));
|
const mod = await import(new URL('./dist/server/entry.mjs', url));
|
||||||
|
if(!mod.running()) {
|
||||||
|
mod.start();
|
||||||
|
}
|
||||||
await cb();
|
await cb();
|
||||||
await mod.stop();
|
await mod.stop();
|
||||||
await close();
|
await close();
|
||||||
|
|
Loading…
Reference in a new issue