-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/deno': patch | ||
--- | ||
|
||
Handles file serving in the main export |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ 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'; | ||
|
||
interface Options { | ||
port?: number; | ||
|
@@ -18,32 +20,43 @@ export function start(manifest: SSRManifest, options: Options) { | |
return; | ||
} | ||
|
||
const clientRoot = new URL('../client/', import.meta.url); | ||
const app = new App(manifest); | ||
|
||
const handler = async (request: Request) => { | ||
const response = await app.render(request); | ||
return response; | ||
if(app.match(request)) { | ||
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({ | ||
port: options.port ?? 8085, | ||
hostname: options.hostname ?? '0.0.0.0', | ||
handler, | ||
//onError: options.onError, | ||
}); | ||
|
||
_startPromise = _server.listenAndServe(); | ||
} | ||
|
||
export function createExports(manifest: SSRManifest) { | ||
export function createExports(manifest: SSRManifest, options: Options) { | ||
const app = new App(manifest); | ||
return { | ||
async stop() { | ||
if (_server) { | ||
_server.close(); | ||
_server = undefined; | ||
} | ||
await Promise.resolve(_startPromise); | ||
}, | ||
running() { | ||
return _server !== undefined; | ||
}, | ||
async start() { | ||
return start(manifest, options); | ||
}, | ||
async handle(request: Request) { | ||
return app.render(request); | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
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({ | ||
name: 'Basics', | ||
async fn() { | ||
await runBuildAndStartApp('./fixtures/basics/', async () => { | ||
await startApp(async () => { | ||
const resp = await fetch('http://127.0.0.1:8085/'); | ||
assertEquals(resp.status, 200); | ||
const html = await resp.text(); | ||
assert(html); | ||
}); | ||
}, | ||
}); | ||
|
||
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() | ||
}); | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from 'https://deno.land/[email protected]/path/mod.ts'; | ||
export * from 'https://deno.land/[email protected]/testing/asserts.ts'; | ||
export * from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters