Implements import.meta.request (#46)

This adds `import.meta.request` to pages (not components).
This commit is contained in:
Matthew Phillips 2021-03-31 16:47:03 -04:00 committed by GitHub
parent d5b15a3851
commit 7c10d563f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 4 deletions

View file

@ -1,12 +1,12 @@
import type { LogOptions } from '../logger.js';
import type { AstroConfig } from '../@types/astro';
import type { AstroConfig, CompileResult, TransformResult } from '../@types/astro';
import path from 'path';
import micromark from 'micromark';
import gfmSyntax from 'micromark-extension-gfm';
import matter from 'gray-matter';
import gfmHtml from 'micromark-extension-gfm/html.js';
import { CompileResult, TransformResult } from '../@types/astro';
import { parse } from '../parser/index.js';
import { createMarkdownHeadersCollector } from '../micromark-collect-headers.js';
import { encodeMarkdown } from '../micromark-encode.js';
@ -130,13 +130,12 @@ export default __render;
// triggered by loading a component directly by URL.
export async function __renderPage({request, children, props}) {
const currentChild = {
setup: typeof setup === 'undefined' ? (passthrough) => passthrough : setup,
layout: typeof __layout === 'undefined' ? undefined : __layout,
content: typeof __content === 'undefined' ? undefined : __content,
__render,
};
await currentChild.setup({request});
import.meta.request = request;
const childBodyResult = await currentChild.__render(props, children);
// find layout, if one was given.

View file

@ -0,0 +1,19 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import { doc } from './test-utils.js';
import { setup } from './helpers.js';
const Request = suite('import.meta.request');
setup(Request, './fixtures/astro-request');
Request('import.meta.request available', async (context) => {
const result = await context.runtime.load('/');
assert.equal(result.statusCode, 200);
const $ = doc(result.contents);
assert.equal($('h1').text(), '/');
});
Request.run();

View file

@ -0,0 +1,5 @@
export default {
projectRoot: '.',
astroRoot: './astro',
dist: './_site'
};

View file

@ -0,0 +1,10 @@
---
let path = import.meta.request.path;
---
<html>
<head><title>Test</title></head>
<body>
<h1>{path}</h1>
</body>
</html>