0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

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
This commit is contained in:
Matthew Phillips 2021-04-27 14:00:33 -04:00 committed by GitHub
parent dea1a6dfc9
commit 41c64b30af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 11 deletions

View file

@ -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

View file

@ -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');
---
<strong>{error}</strong>

View file

@ -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.

View file

@ -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');
---
<!doctype html>

View file

@ -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: [],

View file

@ -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);

View file

@ -8,8 +8,8 @@ import { fileURLToPath } from 'url';
const RSS = suite('RSS Generation');
const snapshot = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[MF Doomcast]]></title><description><![CDATA[The podcast about the things you find on a picnic, or at a picnic table]]></description><link>https://mysite.dev/feed/episodes.xml</link><language>en-us</language><itunes:author>MF Doom</itunes:author><item><title><![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]></title><link>https://mysite.dev/episode/rap-snitch-knishes/</link><description><![CDATA[Complex named this song the “22nd funniest rap song of all time.”]]></description><pubDate>Tue, 16 Nov 2004 07:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>172</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Fazers]]></title><link>https://mysite.dev/episode/fazers/</link><description><![CDATA[Rhapsody ranked Take Me to Your Leader 17th on its list “Hip-Hops Best Albums of the Decade”]]></description><pubDate>Thu, 03 Jul 2003 06:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>197</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]></title><link>https://mysite.dev/episode/rhymes-like-dimes/</link><description><![CDATA[Operation: Doomsday has been heralded as an underground classic that established MF Doom's rank within the underground hip-hop scene during the early to mid-2000s.
]]></description><pubDate>Tue, 19 Oct 1999 06:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>259</itunes:duration><itunes:explicit>true</itunes:explicit></item></channel></rss>`;
const snapshot = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[MF Doomcast]]></title><description><![CDATA[The podcast about the things you find on a picnic, or at a picnic table]]></description><link>https://mysite.dev/feed/episodes.xml</link><language>en-us</language><itunes:author>MF Doom</itunes:author><item><title><![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]></title><link>https://mysite.dev/episode/rap-snitch-knishes/</link><description><![CDATA[Complex named this song the “22nd funniest rap song of all time.”]]></description><pubDate>Tue, 16 Nov 2004 05:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>172</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Fazers]]></title><link>https://mysite.dev/episode/fazers/</link><description><![CDATA[Rhapsody ranked Take Me to Your Leader 17th on its list “Hip-Hops Best Albums of the Decade”]]></description><pubDate>Thu, 03 Jul 2003 04:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>197</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]></title><link>https://mysite.dev/episode/rhymes-like-dimes/</link><description><![CDATA[Operation: Doomsday has been heralded as an underground classic that established MF Doom's rank within the underground hip-hop scene during the early to mid-2000s.
]]></description><pubDate>Tue, 19 Oct 1999 04:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>259</itunes:duration><itunes:explicit>true</itunes:explicit></item></channel></rss>`;
const cwd = new URL('./fixtures/astro-rss', import.meta.url);

View file

@ -1,5 +1,5 @@
---
let path = import.meta.request.path;
let path = Astro.request.url.pathname;
---
<html>