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

Deno fix #6131 #6248

Merged
merged 11 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/funny-seahorses-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/deno': patch
---

Deno SSR with prerender=true complains about invalid URL scheme
14 changes: 8 additions & 6 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter):
},
load(id) {
if (id === resolvedVirtualModuleId) {
return `import * as adapter from '${adapter.serverEntrypoint}';
return `import * as _adapter from '${adapter.serverEntrypoint}';
import * as _main from '${pagesVirtualModuleId}';
import { deserializeManifest as _deserializeManifest } from 'astro/app';
const _manifest = Object.assign(_deserializeManifest('${manifestReplace}'), {
Expand All @@ -47,7 +47,7 @@ const _args = ${adapter.args ? JSON.stringify(adapter.args) : 'undefined'};
export * from '${pagesVirtualModuleId}';
${
adapter.exports
? `const _exports = adapter.createExports(_manifest, _args);
? `const _exports = _adapter.createExports(_manifest, _args);
${adapter.exports
.map((name) => {
if (name === 'default') {
Expand All @@ -61,11 +61,13 @@ export { _default as default };`;
`
: ''
}
export const adapter = _adapter
${adapter.name !== '@astrojs/deno' ? `
const _start = 'start';
if(_start in adapter) {
adapter[_start](_manifest, _args);
}`;
}
if(_start in _adapter) {
_adapter[_start](_manifest, _args);
}`: ''}`;
}
return void 0;
},
async generateBundle(_opts, bundle) {
Expand Down
2 changes: 2 additions & 0 deletions packages/integrations/deno/src/code-constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULTIMPORT = `import { Server } from "https://deno.land/[email protected]/http/server.ts"; \n import { fetch } from "https://deno.land/x/file_fetch/mod.ts";\nimport { fileExtension } from "https://deno.land/x/[email protected]/mod.ts";`
export const DEFAULTSTART = `const _start = 'start'; \n if(_start in adapter) { \nadapter[_start](_manifest, _args);}`
3 changes: 3 additions & 0 deletions packages/integrations/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import esbuild from 'esbuild';
import * as fs from 'fs';
import * as npath from 'path';
import { fileURLToPath } from 'url';
import * as CONSTANT from './code-constant'

interface BuildConfig {
server: URL;
Expand Down Expand Up @@ -70,6 +71,8 @@ export default function createIntegration(args?: Options): AstroIntegration {
'astro:build:done': async () => {
const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
const pth = fileURLToPath(entryUrl);
const content = await fs.readFileSync(pth, 'utf8')
await fs.writeFileSync(pth, `${CONSTANT.DEFAULTIMPORT}${content}${CONSTANT.DEFAULTSTART}`);
await esbuild.build({
target: 'es2020',
platform: 'browser',
Expand Down
22 changes: 16 additions & 6 deletions packages/integrations/deno/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// Normal Imports
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;
hostname?: string;
start?: boolean;
}

// @ts-ignore
let _server: Server | undefined = undefined;
let _startPromise: Promise<void> | undefined = undefined;

Expand Down Expand Up @@ -39,8 +37,19 @@ 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());

const stringLocalPath = localPath.toString();
// @ts-ignore
const extendName = fileExtension(stringLocalPath);
const fileResp = await fetch(
!extendName
? `${
stringLocalPath.endsWith('/')
? `${stringLocalPath}index.html`
: `${stringLocalPath}/index.html`
}`
: stringLocalPath
);

// If the static file can't be found
if (fileResp.status == 404) {
// Render the astro custom 404 page
Expand All @@ -60,6 +69,7 @@ export function start(manifest: SSRManifest, options: Options) {
};

const port = options.port ?? 8085;
// @ts-ignore
_server = new Server({
port,
hostname: options.hostname ?? '0.0.0.0',
Expand Down
21 changes: 21 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,24 @@ 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('perendering', 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>