From 41c64b30af7ed34c1a870b6f273a2889fa2df102 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 27 Apr 2021 14:00:33 -0400 Subject: [PATCH] Move the `request` object from import.meta to Astro (#134) * Move the `request` object from import.meta to Astro This moves the `request` object to the Astro "global" (really just a render-level variable). * Document Astro.request --- docs/api.md | 8 ++++++++ docs/dev.md | 2 +- src/compiler/index.ts | 7 ++++++- src/frontend/500.astro | 2 +- src/runtime.ts | 4 +--- test/astro-request.test.js | 4 ++-- test/astro-rss.test.js | 4 ++-- test/fixtures/astro-request/src/pages/index.astro | 2 +- 8 files changed, 22 insertions(+), 11 deletions(-) diff --git a/docs/api.md b/docs/api.md index 785f9a83ba..d3af1564f0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -43,6 +43,14 @@ const data = Astro.fetchContent('../pages/post/*.md'); // returns an array of po }[]; ``` +#### `request` + +`Astro.request` returns an object with the following properties: + +| Name | Type | Description | +| :----- | :------- | :--------------------------------------------------------------------------------------------------------- | +| `url` | `URL` | The URL of the request being rendered. | + ### `collection` ```jsx diff --git a/docs/dev.md b/docs/dev.md index 7b811d7d3f..8057ddcd6d 100644 --- a/docs/dev.md +++ b/docs/dev.md @@ -42,7 +42,7 @@ The 500 page will receive an `error` query parameter which you can access with: ``` --- -const error = import.meta.request.url.searchParams.get('error'); +const error = Astro.request.url.searchParams.get('error'); --- {error} diff --git a/src/compiler/index.ts b/src/compiler/index.ts index d2d7bff084..7e7bfc4c6b 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -131,7 +131,12 @@ ${result.imports.join('\n')} // \`__render()\`: Render the contents of the Astro module. import { h, Fragment } from '${internalImport('h.js')}'; +const __astroRequestSymbol = Symbol('astro.request'); async function __render(props, ...children) { + const Astro = { + request: props[__astroRequestSymbol] + }; + ${result.script} return h(Fragment, null, ${result.html}); } @@ -148,7 +153,7 @@ export async function __renderPage({request, children, props}) { __render, }; - import.meta.request = request; + props[__astroRequestSymbol] = request; const childBodyResult = await currentChild.__render(props, children); // find layout, if one was given. diff --git a/src/frontend/500.astro b/src/frontend/500.astro index af7b901e9f..01fab8bea6 100644 --- a/src/frontend/500.astro +++ b/src/frontend/500.astro @@ -2,7 +2,7 @@ import Prism from 'astro/components/Prism.astro'; let title = 'Uh oh...'; -const error = import.meta.request.url.searchParams.get('error'); +const error = Astro.request.url.searchParams.get('error'); --- diff --git a/src/runtime.ts b/src/runtime.ts index a394d93c4e..d7f63183c7 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -205,9 +205,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro let html = (await mod.exports.__renderPage({ request: { - host: fullurl.hostname, - path: fullurl.pathname, - href: fullurl.toString(), + // params should go here when implemented url: requestURL }, children: [], diff --git a/test/astro-request.test.js b/test/astro-request.test.js index 05fcf035b0..1156714dd2 100644 --- a/test/astro-request.test.js +++ b/test/astro-request.test.js @@ -3,11 +3,11 @@ import * as assert from 'uvu/assert'; import { doc } from './test-utils.js'; import { setup } from './helpers.js'; -const Request = suite('import.meta.request'); +const Request = suite('Astro.request'); setup(Request, './fixtures/astro-request'); -Request('import.meta.request available', async (context) => { +Request('Astro.request available', async (context) => { const result = await context.runtime.load('/'); assert.equal(result.statusCode, 200); diff --git a/test/astro-rss.test.js b/test/astro-rss.test.js index ed0d378185..61234d36de 100644 --- a/test/astro-rss.test.js +++ b/test/astro-rss.test.js @@ -8,8 +8,8 @@ import { fileURLToPath } from 'url'; const RSS = suite('RSS Generation'); -const snapshot = `<![CDATA[MF Doomcast]]>https://mysite.dev/feed/episodes.xmlen-usMF Doom<![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]>https://mysite.dev/episode/rap-snitch-knishes/Tue, 16 Nov 2004 07:00:00 GMTmusic172true<![CDATA[Fazers]]>https://mysite.dev/episode/fazers/Thu, 03 Jul 2003 06:00:00 GMTmusic197true<![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]>https://mysite.dev/episode/rhymes-like-dimes/Tue, 19 Oct 1999 06:00:00 GMTmusic259true`; +const snapshot = `<![CDATA[MF Doomcast]]>https://mysite.dev/feed/episodes.xmlen-usMF Doom<![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]>https://mysite.dev/episode/rap-snitch-knishes/Tue, 16 Nov 2004 05:00:00 GMTmusic172true<![CDATA[Fazers]]>https://mysite.dev/episode/fazers/Thu, 03 Jul 2003 04:00:00 GMTmusic197true<![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]>https://mysite.dev/episode/rhymes-like-dimes/Tue, 19 Oct 1999 04:00:00 GMTmusic259true`; const cwd = new URL('./fixtures/astro-rss', import.meta.url); diff --git a/test/fixtures/astro-request/src/pages/index.astro b/test/fixtures/astro-request/src/pages/index.astro index 590a5e1549..f809a76e3d 100644 --- a/test/fixtures/astro-request/src/pages/index.astro +++ b/test/fixtures/astro-request/src/pages/index.astro @@ -1,5 +1,5 @@ --- -let path = import.meta.request.path; +let path = Astro.request.url.pathname; ---