From f6567994eb51aeffd15849bc2dda211c8e2dd346 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 14 Feb 2023 08:39:17 +0000 Subject: [PATCH 1/8] fix: #6131 --- .changeset/funny-seahorses-compare.md | 6 ++++++ .../astro/src/core/build/plugins/plugin-ssr.ts | 10 ++++++---- packages/integrations/deno/src/code-constant.ts | 2 ++ packages/integrations/deno/src/index.ts | 14 ++++++++++++-- packages/integrations/deno/src/server.ts | 12 +++++++----- 5 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 .changeset/funny-seahorses-compare.md create mode 100644 packages/integrations/deno/src/code-constant.ts diff --git a/.changeset/funny-seahorses-compare.md b/.changeset/funny-seahorses-compare.md new file mode 100644 index 000000000000..4ae565a82490 --- /dev/null +++ b/.changeset/funny-seahorses-compare.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'@astrojs/deno': patch +--- + +Deno SSR with prerender=true complains about invalid URL scheme diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index d4536b92c8ab..048d0a13088d 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -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) { diff --git a/packages/integrations/deno/src/code-constant.ts b/packages/integrations/deno/src/code-constant.ts new file mode 100644 index 000000000000..aad789529f30 --- /dev/null +++ b/packages/integrations/deno/src/code-constant.ts @@ -0,0 +1,2 @@ +export const DEFAULTIMPORT = `import { Server } from "https://deno.land/std@0.167.0/http/server.ts"; \n import { fetch } from "https://deno.land/x/file_fetch/mod.ts";` +export const DEFAULTSTART = `const _start = 'start'; \n if(_start in _adapter) { \n_adapter[_start](_manifest, _args);}` diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 82da151888cd..9fca99900273 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -3,11 +3,11 @@ 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; serverEntry: string; - assets: string; } interface Options { @@ -32,10 +32,12 @@ export function getAdapter(args?: Options): AstroAdapter { export default function createIntegration(args?: Options): AstroIntegration { let _buildConfig: BuildConfig; let _vite: any; + let needsBuildConfig = false; return { name: '@astrojs/deno', hooks: { 'astro:config:done': ({ setAdapter, config }) => { + needsBuildConfig = !config.build.client; setAdapter(getAdapter(args)); _buildConfig = config.build; @@ -46,6 +48,12 @@ export default function createIntegration(args?: Options): AstroIntegration { ); } }, + 'astro:build:start': ({ buildConfig }) => { + // Backwards compat + if (needsBuildConfig) { + _buildConfig = buildConfig; + } + }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { _vite = vite; @@ -70,6 +78,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}\n${content}\n${CONSTANT.DEFAULTSTART}`); await esbuild.build({ target: 'es2020', platform: 'browser', @@ -87,7 +97,7 @@ export default function createIntegration(args?: Options): AstroIntegration { // Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash. try { const chunkFileNames = - _vite?.build?.rollupOptions?.output?.chunkFileNames ?? `chunks/chunk.[hash].mjs`; + _vite?.build?.rollupOptions?.output?.chunkFileNames ?? 'chunks/chunk.[hash].mjs'; const chunkPath = npath.dirname(chunkFileNames); const chunksDirUrl = new URL(chunkPath + '/', _buildConfig.server); await fs.promises.rm(chunksDirUrl, { recursive: true, force: true }); diff --git a/packages/integrations/deno/src/server.ts b/packages/integrations/deno/src/server.ts index 00e1c43db5cf..62a0b4e95218 100644 --- a/packages/integrations/deno/src/server.ts +++ b/packages/integrations/deno/src/server.ts @@ -1,10 +1,6 @@ // Normal Imports import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; -// @ts-ignore -import { Server } from 'https://deno.land/std@0.167.0/http/server.ts'; -// @ts-ignore -import { fetch } from 'https://deno.land/x/file_fetch/mod.ts'; interface Options { port?: number; @@ -12,6 +8,7 @@ interface Options { start?: boolean; } +// @ts-ignore let _server: Server | undefined = undefined; let _startPromise: Promise | undefined = undefined; @@ -39,7 +36,11 @@ 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 fileResp = await fetch( + localPath.toString().endsWith('client/') + ? `${localPath.toString()}index.html` + : localPath.toString() + ); // If the static file can't be found if (fileResp.status == 404) { @@ -60,6 +61,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', From 8513b0534f27dbf4c965d8ded881ca4d2fa8fa64 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 14 Feb 2023 09:39:08 +0000 Subject: [PATCH 2/8] fix: delete --- packages/integrations/deno/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 9fca99900273..4e0c8495de5c 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -79,7 +79,7 @@ export default function createIntegration(args?: Options): AstroIntegration { 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}\n${content}\n${CONSTANT.DEFAULTSTART}`); + await fs.writeFileSync(pth, `${CONSTANT.DEFAULTIMPORT}${content}${CONSTANT.DEFAULTSTART}`); await esbuild.build({ target: 'es2020', platform: 'browser', From c74efde9717411e944a9293792fbf52b1eade865 Mon Sep 17 00:00:00 2001 From: wuls Date: Tue, 14 Feb 2023 17:50:48 +0800 Subject: [PATCH 3/8] update code --- packages/integrations/deno/src/index.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 4e0c8495de5c..891623ead537 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -8,6 +8,7 @@ import * as CONSTANT from './code-constant' interface BuildConfig { server: URL; serverEntry: string; + assets: string; } interface Options { @@ -32,12 +33,10 @@ export function getAdapter(args?: Options): AstroAdapter { export default function createIntegration(args?: Options): AstroIntegration { let _buildConfig: BuildConfig; let _vite: any; - let needsBuildConfig = false; return { name: '@astrojs/deno', hooks: { 'astro:config:done': ({ setAdapter, config }) => { - needsBuildConfig = !config.build.client; setAdapter(getAdapter(args)); _buildConfig = config.build; @@ -48,12 +47,6 @@ export default function createIntegration(args?: Options): AstroIntegration { ); } }, - 'astro:build:start': ({ buildConfig }) => { - // Backwards compat - if (needsBuildConfig) { - _buildConfig = buildConfig; - } - }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { _vite = vite; @@ -97,7 +90,7 @@ export default function createIntegration(args?: Options): AstroIntegration { // Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash. try { const chunkFileNames = - _vite?.build?.rollupOptions?.output?.chunkFileNames ?? 'chunks/chunk.[hash].mjs'; + _vite?.build?.rollupOptions?.output?.chunkFileNames ?? `chunks/chunk.[hash].mjs`; const chunkPath = npath.dirname(chunkFileNames); const chunksDirUrl = new URL(chunkPath + '/', _buildConfig.server); await fs.promises.rm(chunksDirUrl, { recursive: true, force: true }); From 32c68a6db2906c7fbde5324872d261e432a48afe Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 14 Feb 2023 10:07:23 +0000 Subject: [PATCH 4/8] fix: fix bug --- packages/astro/src/core/build/plugins/plugin-ssr.ts | 4 ++-- packages/integrations/deno/src/code-constant.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 048d0a13088d..58548c0bb677 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -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}'), { @@ -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') { diff --git a/packages/integrations/deno/src/code-constant.ts b/packages/integrations/deno/src/code-constant.ts index aad789529f30..cd551955bda3 100644 --- a/packages/integrations/deno/src/code-constant.ts +++ b/packages/integrations/deno/src/code-constant.ts @@ -1,2 +1,2 @@ export const DEFAULTIMPORT = `import { Server } from "https://deno.land/std@0.167.0/http/server.ts"; \n import { fetch } from "https://deno.land/x/file_fetch/mod.ts";` -export const DEFAULTSTART = `const _start = 'start'; \n if(_start in _adapter) { \n_adapter[_start](_manifest, _args);}` +export const DEFAULTSTART = `const _start = 'start'; \n if(_start in adapter) { \nadapter[_start](_manifest, _args);}` From abed25a0636c2091d3fb5e66d261e665fdb983e1 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 14 Feb 2023 10:21:27 +0000 Subject: [PATCH 5/8] add some tests --- .../integrations/deno/test/basics.test.ts | 250 ++++++++++-------- .../basics/src/pages/perendering.astro | 9 + 2 files changed, 145 insertions(+), 114 deletions(-) create mode 100644 packages/integrations/deno/test/fixtures/basics/src/pages/perendering.astro diff --git a/packages/integrations/deno/test/basics.test.ts b/packages/integrations/deno/test/basics.test.ts index d1f8907cb3c7..d6257cbe49a1 100644 --- a/packages/integrations/deno/test/basics.test.ts +++ b/packages/integrations/deno/test/basics.test.ts @@ -12,132 +12,154 @@ async function startApp(cb: StartServerCallback) { const varContent = 'this is a value stored in env variable'; Deno.env.set('SOME_VARIABLE', varContent); -Deno.test({ - name: 'Basics', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(baseUrl); - assertEquals(resp.status, 200); - - const html = await resp.text(); - assert(html); - - const doc = new DOMParser().parseFromString(html, `text/html`); - const div = doc!.querySelector('#react'); - - assert(div, 'div exists'); - }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); +// Deno.test({ +// name: 'Basics', +// permissions: defaultTestPermissions, +// async fn() { +// await startApp(async (baseUrl: URL) => { +// const resp = await fetch(baseUrl); +// assertEquals(resp.status, 200); + +// const html = await resp.text(); +// assert(html); + +// const doc = new DOMParser().parseFromString(html, `text/html`); +// const div = doc!.querySelector('#react'); + +// assert(div, 'div exists'); +// }); +// }, +// sanitizeResources: false, +// sanitizeOps: false, +// }); + +// Deno.test({ +// name: 'Custom 404', +// permissions: defaultTestPermissions, +// async fn() { +// await startApp(async (baseUrl: URL) => { +// const resp = await fetch(new URL('this-does-not-exist', baseUrl)); +// assertEquals(resp.status, 404); + +// const html = await resp.text(); +// assert(html); + +// const doc = new DOMParser().parseFromString(html, `text/html`); +// const header = doc!.querySelector('#custom-404'); +// assert(header, 'displays custom 404'); +// }); +// }, +// sanitizeResources: false, +// sanitizeOps: false, +// }); + +// Deno.test({ +// name: 'Loads style assets', +// permissions: defaultTestPermissions, +// async fn() { +// await startApp(async (baseUrl: URL) => { +// let resp = await fetch(baseUrl); +// 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!, baseUrl)); +// assertEquals(resp.status, 200); +// const ct = resp.headers.get('content-type'); +// assertEquals(ct, 'text/css'); +// await resp.body!.cancel(); +// }); +// }, +// sanitizeResources: false, +// sanitizeOps: false, +// }); + +// Deno.test({ +// name: 'Correctly loads run-time env variables', +// permissions: defaultTestPermissions, +// async fn() { +// await startApp(async (baseUrl: URL) => { +// const resp = await fetch(baseUrl); +// const html = await resp.text(); + +// const doc = new DOMParser().parseFromString(html, `text/html`); +// const p = doc!.querySelector('p#env-value'); +// assertEquals(p!.innerText, varContent); +// }); +// }, +// sanitizeResources: false, +// sanitizeOps: false, +// }); + +// Deno.test({ +// name: 'Works with Markdown', +// permissions: defaultTestPermissions, +// async fn() { +// await startApp(async (baseUrl: URL) => { +// const resp = await fetch(new URL('markdown', baseUrl)); +// const html = await resp.text(); + +// const doc = new DOMParser().parseFromString(html, `text/html`); +// const h1 = doc!.querySelector('h1'); +// assertEquals(h1!.innerText, 'Heading from Markdown'); +// }); +// }, +// sanitizeResources: false, +// sanitizeOps: false, +// }); + +// Deno.test({ +// name: 'Works with MDX', +// permissions: defaultTestPermissions, +// async fn() { +// await startApp(async (baseUrl: URL) => { +// const resp = await fetch(new URL('mdx', baseUrl)); +// const html = await resp.text(); + +// const doc = new DOMParser().parseFromString(html, `text/html`); +// const h1 = doc!.querySelector('h1'); +// assertEquals(h1!.innerText, 'Heading from MDX'); +// }); +// }, +// sanitizeResources: false, +// sanitizeOps: false, +// }); + +// Deno.test({ +// name: 'Astro.cookies', +// permissions: defaultTestPermissions, +// async fn() { +// await startApp(async (baseUrl: URL) => { +// const url = new URL('/admin', baseUrl); +// const resp = await fetch(url, { redirect: 'manual' }); +// assertEquals(resp.status, 302); + +// const headers = resp.headers; +// assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/'); +// }); +// }, +// sanitizeResources: false, +// sanitizeOps: false, +// }); Deno.test({ - name: 'Custom 404', + name: 'perendering', permissions: defaultTestPermissions, async fn() { await startApp(async (baseUrl: URL) => { - const resp = await fetch(new URL('this-does-not-exist', baseUrl)); - assertEquals(resp.status, 404); - - const html = await resp.text(); - assert(html); - - const doc = new DOMParser().parseFromString(html, `text/html`); - const header = doc!.querySelector('#custom-404'); - assert(header, 'displays custom 404'); - }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); - -Deno.test({ - name: 'Loads style assets', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - let resp = await fetch(baseUrl); - 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!, baseUrl)); + const resp = await fetch(new URL('perendering', baseUrl)); assertEquals(resp.status, 200); - const ct = resp.headers.get('content-type'); - assertEquals(ct, 'text/css'); - await resp.body!.cancel(); - }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); - -Deno.test({ - name: 'Correctly loads run-time env variables', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(baseUrl); - const html = await resp.text(); - const doc = new DOMParser().parseFromString(html, `text/html`); - const p = doc!.querySelector('p#env-value'); - assertEquals(p!.innerText, varContent); - }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); -Deno.test({ - name: 'Works with Markdown', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(new URL('markdown', baseUrl)); - const html = await resp.text(); - - const doc = new DOMParser().parseFromString(html, `text/html`); - const h1 = doc!.querySelector('h1'); - assertEquals(h1!.innerText, 'Heading from Markdown'); - }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); - -Deno.test({ - name: 'Works with MDX', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const resp = await fetch(new URL('mdx', baseUrl)); const html = await resp.text(); + assert(html); const doc = new DOMParser().parseFromString(html, `text/html`); const h1 = doc!.querySelector('h1'); - assertEquals(h1!.innerText, 'Heading from MDX'); - }); - }, - sanitizeResources: false, - sanitizeOps: false, -}); - -Deno.test({ - name: 'Astro.cookies', - permissions: defaultTestPermissions, - async fn() { - await startApp(async (baseUrl: URL) => { - const url = new URL('/admin', baseUrl); - const resp = await fetch(url, { redirect: 'manual' }); - assertEquals(resp.status, 302); - - const headers = resp.headers; - assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/'); + console.log('>>', h1) + assertEquals(h1!.innerText, 'test'); }); }, sanitizeResources: false, diff --git a/packages/integrations/deno/test/fixtures/basics/src/pages/perendering.astro b/packages/integrations/deno/test/fixtures/basics/src/pages/perendering.astro new file mode 100644 index 000000000000..ca317d38d296 --- /dev/null +++ b/packages/integrations/deno/test/fixtures/basics/src/pages/perendering.astro @@ -0,0 +1,9 @@ +--- +export const prerender = true; +--- + + + +

test

+ + From 2a187f089cc54aff1ecef550e99b21cd403b1a88 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Wed, 15 Feb 2023 02:39:45 +0000 Subject: [PATCH 6/8] fix route error --- packages/integrations/deno/src/code-constant.ts | 2 +- packages/integrations/deno/src/server.ts | 15 +++++++++++---- .../deno/test/fixtures/basics/package.json | 3 +++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/integrations/deno/src/code-constant.ts b/packages/integrations/deno/src/code-constant.ts index cd551955bda3..bf272e397080 100644 --- a/packages/integrations/deno/src/code-constant.ts +++ b/packages/integrations/deno/src/code-constant.ts @@ -1,2 +1,2 @@ -export const DEFAULTIMPORT = `import { Server } from "https://deno.land/std@0.167.0/http/server.ts"; \n import { fetch } from "https://deno.land/x/file_fetch/mod.ts";` +export const DEFAULTIMPORT = `import { Server } from "https://deno.land/std@0.167.0/http/server.ts"; \n import { fetch } from "https://deno.land/x/file_fetch/mod.ts";\nimport { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";` export const DEFAULTSTART = `const _start = 'start'; \n if(_start in adapter) { \nadapter[_start](_manifest, _args);}` diff --git a/packages/integrations/deno/src/server.ts b/packages/integrations/deno/src/server.ts index 62a0b4e95218..0a3c7c85c0d4 100644 --- a/packages/integrations/deno/src/server.ts +++ b/packages/integrations/deno/src/server.ts @@ -2,6 +2,7 @@ import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; + interface Options { port?: number; hostname?: string; @@ -36,12 +37,18 @@ 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 stringLocalPath = localPath.toString(); + // @ts-ignore + const extendName = fileExtension(stringLocalPath); + console.log(!extendName + ? `${stringLocalPath}index.html` + : stringLocalPath) const fileResp = await fetch( - localPath.toString().endsWith('client/') - ? `${localPath.toString()}index.html` - : localPath.toString() + !extendName + ? `${stringLocalPath}index.html` + : stringLocalPath ); - + // If the static file can't be found if (fileResp.status == 404) { // Render the astro custom 404 page diff --git a/packages/integrations/deno/test/fixtures/basics/package.json b/packages/integrations/deno/test/fixtures/basics/package.json index ebb4270b2309..562bef13d902 100644 --- a/packages/integrations/deno/test/fixtures/basics/package.json +++ b/packages/integrations/deno/test/fixtures/basics/package.json @@ -9,5 +9,8 @@ "@astrojs/mdx": "workspace:*", "react": "^18.1.0", "react-dom": "^18.1.0" + }, + "scripts": { + "preview": "deno run --allow-net --allow-read --allow-env ./dist/server/entry.mjs" } } From 7d4a92d9a60d49593f28538eecf2cd07a2cead75 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Wed, 15 Feb 2023 02:57:35 +0000 Subject: [PATCH 7/8] delete comment --- packages/integrations/deno/src/server.ts | 11 +- .../integrations/deno/test/basics.test.ts | 263 +++++++++--------- 2 files changed, 137 insertions(+), 137 deletions(-) diff --git a/packages/integrations/deno/src/server.ts b/packages/integrations/deno/src/server.ts index 0a3c7c85c0d4..ed358f2f09dc 100644 --- a/packages/integrations/deno/src/server.ts +++ b/packages/integrations/deno/src/server.ts @@ -40,15 +40,16 @@ export function start(manifest: SSRManifest, options: Options) { const stringLocalPath = localPath.toString(); // @ts-ignore const extendName = fileExtension(stringLocalPath); - console.log(!extendName - ? `${stringLocalPath}index.html` - : stringLocalPath) const fileResp = await fetch( !extendName - ? `${stringLocalPath}index.html` + ? `${ + 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 diff --git a/packages/integrations/deno/test/basics.test.ts b/packages/integrations/deno/test/basics.test.ts index d6257cbe49a1..e61dbd3f7a35 100644 --- a/packages/integrations/deno/test/basics.test.ts +++ b/packages/integrations/deno/test/basics.test.ts @@ -12,137 +12,137 @@ async function startApp(cb: StartServerCallback) { const varContent = 'this is a value stored in env variable'; Deno.env.set('SOME_VARIABLE', varContent); -// Deno.test({ -// name: 'Basics', -// permissions: defaultTestPermissions, -// async fn() { -// await startApp(async (baseUrl: URL) => { -// const resp = await fetch(baseUrl); -// assertEquals(resp.status, 200); - -// const html = await resp.text(); -// assert(html); - -// const doc = new DOMParser().parseFromString(html, `text/html`); -// const div = doc!.querySelector('#react'); - -// assert(div, 'div exists'); -// }); -// }, -// sanitizeResources: false, -// sanitizeOps: false, -// }); - -// Deno.test({ -// name: 'Custom 404', -// permissions: defaultTestPermissions, -// async fn() { -// await startApp(async (baseUrl: URL) => { -// const resp = await fetch(new URL('this-does-not-exist', baseUrl)); -// assertEquals(resp.status, 404); - -// const html = await resp.text(); -// assert(html); - -// const doc = new DOMParser().parseFromString(html, `text/html`); -// const header = doc!.querySelector('#custom-404'); -// assert(header, 'displays custom 404'); -// }); -// }, -// sanitizeResources: false, -// sanitizeOps: false, -// }); - -// Deno.test({ -// name: 'Loads style assets', -// permissions: defaultTestPermissions, -// async fn() { -// await startApp(async (baseUrl: URL) => { -// let resp = await fetch(baseUrl); -// 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!, baseUrl)); -// assertEquals(resp.status, 200); -// const ct = resp.headers.get('content-type'); -// assertEquals(ct, 'text/css'); -// await resp.body!.cancel(); -// }); -// }, -// sanitizeResources: false, -// sanitizeOps: false, -// }); - -// Deno.test({ -// name: 'Correctly loads run-time env variables', -// permissions: defaultTestPermissions, -// async fn() { -// await startApp(async (baseUrl: URL) => { -// const resp = await fetch(baseUrl); -// const html = await resp.text(); - -// const doc = new DOMParser().parseFromString(html, `text/html`); -// const p = doc!.querySelector('p#env-value'); -// assertEquals(p!.innerText, varContent); -// }); -// }, -// sanitizeResources: false, -// sanitizeOps: false, -// }); - -// Deno.test({ -// name: 'Works with Markdown', -// permissions: defaultTestPermissions, -// async fn() { -// await startApp(async (baseUrl: URL) => { -// const resp = await fetch(new URL('markdown', baseUrl)); -// const html = await resp.text(); - -// const doc = new DOMParser().parseFromString(html, `text/html`); -// const h1 = doc!.querySelector('h1'); -// assertEquals(h1!.innerText, 'Heading from Markdown'); -// }); -// }, -// sanitizeResources: false, -// sanitizeOps: false, -// }); - -// Deno.test({ -// name: 'Works with MDX', -// permissions: defaultTestPermissions, -// async fn() { -// await startApp(async (baseUrl: URL) => { -// const resp = await fetch(new URL('mdx', baseUrl)); -// const html = await resp.text(); - -// const doc = new DOMParser().parseFromString(html, `text/html`); -// const h1 = doc!.querySelector('h1'); -// assertEquals(h1!.innerText, 'Heading from MDX'); -// }); -// }, -// sanitizeResources: false, -// sanitizeOps: false, -// }); - -// Deno.test({ -// name: 'Astro.cookies', -// permissions: defaultTestPermissions, -// async fn() { -// await startApp(async (baseUrl: URL) => { -// const url = new URL('/admin', baseUrl); -// const resp = await fetch(url, { redirect: 'manual' }); -// assertEquals(resp.status, 302); - -// const headers = resp.headers; -// assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/'); -// }); -// }, -// sanitizeResources: false, -// sanitizeOps: false, -// }); +Deno.test({ + name: 'Basics', + permissions: defaultTestPermissions, + async fn() { + await startApp(async (baseUrl: URL) => { + const resp = await fetch(baseUrl); + assertEquals(resp.status, 200); + + const html = await resp.text(); + assert(html); + + const doc = new DOMParser().parseFromString(html, `text/html`); + const div = doc!.querySelector('#react'); + + assert(div, 'div exists'); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: 'Custom 404', + permissions: defaultTestPermissions, + async fn() { + await startApp(async (baseUrl: URL) => { + const resp = await fetch(new URL('this-does-not-exist', baseUrl)); + assertEquals(resp.status, 404); + + const html = await resp.text(); + assert(html); + + const doc = new DOMParser().parseFromString(html, `text/html`); + const header = doc!.querySelector('#custom-404'); + assert(header, 'displays custom 404'); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: 'Loads style assets', + permissions: defaultTestPermissions, + async fn() { + await startApp(async (baseUrl: URL) => { + let resp = await fetch(baseUrl); + 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!, baseUrl)); + assertEquals(resp.status, 200); + const ct = resp.headers.get('content-type'); + assertEquals(ct, 'text/css'); + await resp.body!.cancel(); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: 'Correctly loads run-time env variables', + permissions: defaultTestPermissions, + async fn() { + await startApp(async (baseUrl: URL) => { + const resp = await fetch(baseUrl); + const html = await resp.text(); + + const doc = new DOMParser().parseFromString(html, `text/html`); + const p = doc!.querySelector('p#env-value'); + assertEquals(p!.innerText, varContent); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: 'Works with Markdown', + permissions: defaultTestPermissions, + async fn() { + await startApp(async (baseUrl: URL) => { + const resp = await fetch(new URL('markdown', baseUrl)); + const html = await resp.text(); + + const doc = new DOMParser().parseFromString(html, `text/html`); + const h1 = doc!.querySelector('h1'); + assertEquals(h1!.innerText, 'Heading from Markdown'); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: 'Works with MDX', + permissions: defaultTestPermissions, + async fn() { + await startApp(async (baseUrl: URL) => { + const resp = await fetch(new URL('mdx', baseUrl)); + const html = await resp.text(); + + const doc = new DOMParser().parseFromString(html, `text/html`); + const h1 = doc!.querySelector('h1'); + assertEquals(h1!.innerText, 'Heading from MDX'); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); + +Deno.test({ + name: 'Astro.cookies', + permissions: defaultTestPermissions, + async fn() { + await startApp(async (baseUrl: URL) => { + const url = new URL('/admin', baseUrl); + const resp = await fetch(url, { redirect: 'manual' }); + assertEquals(resp.status, 302); + + const headers = resp.headers; + assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/'); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +}); Deno.test({ name: 'perendering', @@ -158,7 +158,6 @@ Deno.test({ const doc = new DOMParser().parseFromString(html, `text/html`); const h1 = doc!.querySelector('h1'); - console.log('>>', h1) assertEquals(h1!.innerText, 'test'); }); }, From 758e3376e0139f1e39c0f873e686fb941fd209d0 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Wed, 15 Feb 2023 03:00:56 +0000 Subject: [PATCH 8/8] delete trash code --- packages/integrations/deno/test/fixtures/basics/package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/integrations/deno/test/fixtures/basics/package.json b/packages/integrations/deno/test/fixtures/basics/package.json index 562bef13d902..ebb4270b2309 100644 --- a/packages/integrations/deno/test/fixtures/basics/package.json +++ b/packages/integrations/deno/test/fixtures/basics/package.json @@ -9,8 +9,5 @@ "@astrojs/mdx": "workspace:*", "react": "^18.1.0", "react-dom": "^18.1.0" - }, - "scripts": { - "preview": "deno run --allow-net --allow-read --allow-env ./dist/server/entry.mjs" } }