summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2022-04-07 15:17:05 -0400
committerGravatar GitHub <noreply@github.com> 2022-04-07 15:17:05 -0400
commit7e9d82d75e4b19993b4246d3546169c8476702c4 (patch)
tree138e04f367467ccf04b91d6b45ca93f8dded04ec
parent42e0e0fa5cfbdf3931470cc924ea065a765f29f5 (diff)
downloadastro-7e9d82d75e4b19993b4246d3546169c8476702c4.tar.gz
astro-7e9d82d75e4b19993b4246d3546169c8476702c4.tar.zst
astro-7e9d82d75e4b19993b4246d3546169c8476702c4.zip
Warn when trying to access headers in SSG mode (#3021)
* Warn when trying to access headers in SSG mode * Adds a changeset * Warn when accessing headers at all + a test
-rw-r--r--.changeset/odd-squids-rest.md5
-rw-r--r--packages/astro/src/core/build/generate.ts5
-rw-r--r--packages/astro/src/core/request.ts18
-rw-r--r--packages/astro/src/vite-plugin-astro-server/index.ts1
-rw-r--r--packages/astro/src/vite-plugin-build-html/index.ts1
-rw-r--r--packages/astro/test/fixtures/static build/src/pages/index.astro3
-rw-r--r--packages/astro/test/static-build.test.js30
7 files changed, 60 insertions, 3 deletions
diff --git a/.changeset/odd-squids-rest.md b/.changeset/odd-squids-rest.md
new file mode 100644
index 000000000..fa2c11c58
--- /dev/null
+++ b/.changeset/odd-squids-rest.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Warn when attempting to access headers in SSG mode
diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts
index 909c0f65a..974e2acd6 100644
--- a/packages/astro/src/core/build/generate.ts
+++ b/packages/astro/src/core/build/generate.ts
@@ -191,6 +191,7 @@ async function generatePath(
}
}
+ const ssr = isBuildingToSSR(opts.astroConfig);
const url = new URL(origin + pathname);
const options: RenderOptions = {
legacyBuild: false,
@@ -219,13 +220,13 @@ async function generatePath(
const fullyRelativePath = relPath[0] === '.' ? relPath : './' + relPath;
return fullyRelativePath;
},
- request: createRequest({ url, headers: new Headers(), logging }),
+ request: createRequest({ url, headers: new Headers(), logging, ssr }),
route: pageData.route,
routeCache,
site: astroConfig.site
? new URL(astroConfig.base, astroConfig.site).toString()
: astroConfig.site,
- ssr: isBuildingToSSR(opts.astroConfig),
+ ssr,
};
let body: string;
diff --git a/packages/astro/src/core/request.ts b/packages/astro/src/core/request.ts
index abccbf995..c18d4f2ca 100644
--- a/packages/astro/src/core/request.ts
+++ b/packages/astro/src/core/request.ts
@@ -11,6 +11,7 @@ export interface CreateRequestOptions {
method?: string;
body?: RequestBody | undefined;
logging: LogOptions;
+ ssr: boolean;
}
export function createRequest({
@@ -19,6 +20,7 @@ export function createRequest({
method = 'GET',
body = undefined,
logging,
+ ssr
}: CreateRequestOptions): Request {
let headersObj =
headers instanceof Headers
@@ -50,5 +52,21 @@ export function createRequest({
},
});
+ if(!ssr) {
+ // Warn when accessing headers in SSG mode
+ const _headers = request.headers;
+ const headersDesc = Object.getOwnPropertyDescriptor(request, 'headers') || {};
+ Object.defineProperty(request, 'headers', {
+ ...headersDesc,
+ get() {
+ warn(logging,
+ 'ssg',
+ `Headers are not exposed in static-site generation (SSG) mode. To enable reading headers you need to set an SSR adapter in your config.`
+ );
+ return _headers;
+ }
+ })
+ }
+
return request;
}
diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts
index dba3b861b..1838a5ad4 100644
--- a/packages/astro/src/vite-plugin-astro-server/index.ts
+++ b/packages/astro/src/vite-plugin-astro-server/index.ts
@@ -176,6 +176,7 @@ async function handleRequest(
method: req.method,
body,
logging,
+ ssr: buildingToSSR
});
try {
diff --git a/packages/astro/src/vite-plugin-build-html/index.ts b/packages/astro/src/vite-plugin-build-html/index.ts
index 9221028f2..34f7b3730 100644
--- a/packages/astro/src/vite-plugin-build-html/index.ts
+++ b/packages/astro/src/vite-plugin-build-html/index.ts
@@ -114,6 +114,7 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin {
url: new URL(origin + pathname),
headers: new Headers(),
logging,
+ ssr: false,
}),
mode: 'production',
origin,
diff --git a/packages/astro/test/fixtures/static build/src/pages/index.astro b/packages/astro/test/fixtures/static build/src/pages/index.astro
index 763046d0a..5bd2410ac 100644
--- a/packages/astro/test/fixtures/static build/src/pages/index.astro
+++ b/packages/astro/test/fixtures/static build/src/pages/index.astro
@@ -3,6 +3,9 @@ import MainHead from '../components/MainHead.astro';
import Nav from '../components/Nav/index.jsx';
import { test as ssrConfigTest } from '@test/static-build-pkg';
let allPosts = await Astro.glob('./posts/*.md');
+
+// Note that this just tests a warning
+Astro.request.headers;
---
<html>
<head>
diff --git a/packages/astro/test/static-build.test.js b/packages/astro/test/static-build.test.js
index 55ce6ca07..e56f40077 100644
--- a/packages/astro/test/static-build.test.js
+++ b/packages/astro/test/static-build.test.js
@@ -6,14 +6,32 @@ function addLeadingSlash(path) {
return path.startsWith('/') ? path : '/' + path;
}
+/**
+ * @typedef {import('../src/core/logger/core').LogMessage} LogMessage
+ */
+
describe('Static build', () => {
+ /** @type {import('./test-utils').Fixture} */
let fixture;
+ /** @type {LogMessage[]} */
+ let logs = [];
before(async () => {
+ /** @type {import('../src/core/logger/core').LogOptions} */
+ const logging = {
+ dest: {
+ write(chunk) {
+ logs.push(chunk);
+ }
+ },
+ level: 'warn',
+ };
+
+
fixture = await loadFixture({
root: './fixtures/static build/',
});
- await fixture.build();
+ await fixture.build({ logging });
});
it('Builds out .astro pages', async () => {
@@ -137,4 +155,14 @@ describe('Static build', () => {
const $ = cheerioLoad(html);
expect($('#ssr-config').text()).to.equal('testing');
});
+
+ it('warns when accessing headers', async () => {
+ let found = false;
+ for(const log of logs) {
+ if(log.type === 'ssg' && /[hH]eaders are not exposed in static-site generation/.test(log.args[0])) {
+ found = true;
+ }
+ }
+ expect(found).to.equal(true, 'Found the log message');
+ });
});