Skip to content

Commit

Permalink
Merge branch 'main' into fix/7080--plt-552
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Jul 17, 2023
2 parents 3cad421 + 5e26faa commit 873b401
Show file tree
Hide file tree
Showing 68 changed files with 725 additions and 395 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-weeks-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Throw an error when `build.split` is set to `true` but `output` isn't set to `"server"`.
10 changes: 10 additions & 0 deletions .changeset/great-days-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@astrojs/netlify': minor
---

When a project uses the new option Astro `build.excludeMiddleware`, the
`@astrojs/netlify/functions` adapter will automatically create an Edge Middleware
that will automatically communicate with the Astro Middleware.

Check the [documentation](https://github.com/withastro/astro/blob/main/packages/integrations/netlify/README.md#edge-middleware-with-astro-middleware) for more details.

5 changes: 5 additions & 0 deletions .changeset/large-meals-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Fix a bug where asset redirects caused Cloudflare error
5 changes: 5 additions & 0 deletions .changeset/light-jars-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Handle inlining non-string boolean environment variables
5 changes: 5 additions & 0 deletions .changeset/six-baboons-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Fix build error when passing `includeFiles`
5 changes: 5 additions & 0 deletions .changeset/spicy-pugs-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Fix runtime env var handling
5 changes: 5 additions & 0 deletions .changeset/sweet-bats-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Fix bug where `.ts` files are not renamed to `.js`
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ jobs:
- name: Use Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.34.1
deno-version: v1.35.0

- name: Install dependencies
run: pnpm install
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@astrojs/mdx": "^0.19.7",
"@astrojs/rss": "^2.4.3",
"@astrojs/sitemap": "^1.3.3",
"@astrojs/sitemap": "^1.4.0",
"astro": "^2.8.3"
}
}
4 changes: 2 additions & 2 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@
"postbuild": "astro-scripts copy \"src/**/*.astro\" && astro-scripts copy \"src/**/*.wasm\"",
"test:unit": "mocha --exit --timeout 30000 ./test/units/**/*.test.js",
"test:unit:match": "mocha --exit --timeout 30000 ./test/units/**/*.test.js -g",
"test": "pnpm run test:unit && mocha --exit --timeout 20000 --ignore **/lit-element.test.js && mocha --timeout 20000 **/lit-element.test.js",
"test:match": "mocha --timeout 20000 -g",
"test": "pnpm run test:unit && mocha --exit --timeout 30000 --ignore **/lit-element.test.js && mocha --timeout 30000 **/lit-element.test.js",
"test:match": "mocha --timeout 30000 -g",
"test:e2e": "playwright test",
"test:e2e:match": "playwright test -g"
},
Expand Down
38 changes: 4 additions & 34 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ import mime from 'mime';
import type {
EndpointHandler,
ManifestData,
MiddlewareResponseHandler,
RouteData,
SSRElement,
SSRManifest,
} from '../../@types/astro';
import type { SinglePageBuiltModule } from '../build/types';
import { attachToResponse, getSetCookiesFromResponse } from '../cookies/index.js';
import { callEndpoint, createAPIContext } from '../endpoint/index.js';
import { callEndpoint } from '../endpoint/index.js';
import { consoleLogDestination } from '../logger/console.js';
import { error, type LogOptions } from '../logger/core.js';
import { callMiddleware } from '../middleware/callMiddleware.js';
import { prependForwardSlash, removeTrailingForwardSlash } from '../path.js';
import { RedirectSinglePageBuiltModule } from '../redirects/index.js';
import {
createEnvironment,
createRenderContext,
renderPage,
tryRenderPage,
type Environment,
} from '../render/index.js';
import { RouteCache } from '../render/route-cache.js';
Expand Down Expand Up @@ -256,36 +254,8 @@ export class App {
env: this.#env,
});

const apiContext = createAPIContext({
request: renderContext.request,
params: renderContext.params,
props: renderContext.props,
site: this.#env.site,
adapterName: this.#env.adapterName,
});
let response;
if (page.onRequest) {
response = await callMiddleware<Response>(
this.#env.logging,
page.onRequest as MiddlewareResponseHandler,
apiContext,
() => {
return renderPage({
mod,
renderContext,
env: this.#env,
cookies: apiContext.cookies,
});
}
);
} else {
response = await renderPage({
mod,
renderContext,
env: this.#env,
cookies: apiContext.cookies,
});
}
const response = await tryRenderPage(renderContext, this.#env, mod, page.onRequest);

Reflect.set(request, responseSentSymbol, true);
return response;
} catch (err: any) {
Expand Down
37 changes: 3 additions & 34 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
GetStaticPathsItem,
ImageTransform,
MiddlewareHandler,
MiddlewareResponseHandler,
RouteData,
RouteType,
SSRError,
Expand All @@ -38,16 +37,15 @@ import {
import { runHookBuildGenerated } from '../../integrations/index.js';
import { isServerLikeOutput } from '../../prerender/utils.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { callEndpoint, createAPIContext, throwIfRedirectNotAllowed } from '../endpoint/index.js';
import { callEndpoint, throwIfRedirectNotAllowed } from '../endpoint/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { debug, info } from '../logger/core.js';
import { callMiddleware } from '../middleware/callMiddleware.js';
import {
getRedirectLocationOrThrow,
RedirectSinglePageBuiltModule,
routeIsRedirect,
} from '../redirects/index.js';
import { createEnvironment, createRenderContext, renderPage } from '../render/index.js';
import { createEnvironment, createRenderContext, tryRenderPage } from '../render/index.js';
import { callGetStaticPaths } from '../render/route-cache.js';
import {
createAssetLink,
Expand Down Expand Up @@ -575,36 +573,7 @@ async function generatePath(
} else {
let response: Response;
try {
const apiContext = createAPIContext({
request: renderContext.request,
params: renderContext.params,
props: renderContext.props,
site: env.site,
adapterName: env.adapterName,
});

if (onRequest) {
response = await callMiddleware<Response>(
env.logging,
onRequest as MiddlewareResponseHandler,
apiContext,
() => {
return renderPage({
mod,
renderContext,
env,
cookies: apiContext.cookies,
});
}
);
} else {
response = await renderPage({
mod,
renderContext,
env,
cookies: apiContext.cookies,
});
}
response = await tryRenderPage(renderContext, env, mod, onRequest);
} catch (err) {
if (!AstroError.is(err) && !(err as SSRError).id && typeof err === 'object') {
(err as SSRError).id = pageData.component;
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ class AstroBuilder {
);
}
}

if (config.build.split === true) {
if (config.output !== 'server') {
throw new Error(
'The option `build.split` can only be used when `output` is set to `"server"`.'
);
}
}
}

/** Stats */
Expand Down
56 changes: 54 additions & 2 deletions packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { AstroCookies, ComponentInstance } from '../../@types/astro';
import type {
AstroCookies,
ComponentInstance,
MiddlewareHandler,
MiddlewareResponseHandler,
} from '../../@types/astro';
import { renderPage as runtimeRenderPage } from '../../runtime/server/index.js';
import { attachToResponse } from '../cookies/index.js';
import { createAPIContext } from '../endpoint/index.js';
import { callMiddleware } from '../middleware/callMiddleware.js';
import { redirectRouteGenerate, redirectRouteStatus, routeIsRedirect } from '../redirects/index.js';
import type { RenderContext } from './context.js';
import type { Environment } from './environment.js';
Expand All @@ -13,7 +20,7 @@ export type RenderPage = {
cookies: AstroCookies;
};

export async function renderPage({ mod, renderContext, env, cookies }: RenderPage) {
async function renderPage({ mod, renderContext, env, cookies }: RenderPage) {
if (routeIsRedirect(renderContext.route)) {
return new Response(null, {
status: redirectRouteStatus(renderContext.route, renderContext.request.method),
Expand Down Expand Up @@ -72,3 +79,48 @@ export async function renderPage({ mod, renderContext, env, cookies }: RenderPag

return response;
}

/**
* It attempts to render a page.
*
* ## Errors
*
* It throws an error if the page can't be rendered.
*/
export async function tryRenderPage<MiddlewareReturnType = Response>(
renderContext: Readonly<RenderContext>,
env: Readonly<Environment>,
mod: Readonly<ComponentInstance>,
onRequest?: MiddlewareHandler<MiddlewareReturnType>
): Promise<Response> {
const apiContext = createAPIContext({
request: renderContext.request,
params: renderContext.params,
props: renderContext.props,
site: env.site,
adapterName: env.adapterName,
});

if (onRequest) {
return await callMiddleware<Response>(
env.logging,
onRequest as MiddlewareResponseHandler,
apiContext,
() => {
return renderPage({
mod,
renderContext,
env,
cookies: apiContext.cookies,
});
}
);
} else {
return await renderPage({
mod,
renderContext,
env,
cookies: apiContext.cookies,
});
}
}
32 changes: 3 additions & 29 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import type {
SSRElement,
} from '../../../@types/astro';
import { PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js';
import { createAPIContext } from '../../endpoint/index.js';
import { enhanceViteSSRError } from '../../errors/dev/index.js';
import { AggregateError, CSSError, MarkdownError } from '../../errors/index.js';
import { callMiddleware } from '../../middleware/callMiddleware.js';
import { isPage, resolveIdToUrl, viteID } from '../../util.js';
import { createRenderContext, loadRenderers, renderPage as coreRenderPage } from '../index.js';
import { createRenderContext, loadRenderers, tryRenderPage } from '../index.js';
import { getStylesForURL } from './css.js';
import type { DevelopmentEnvironment } from './environment';
import { getComponentMetadata } from './metadata.js';
Expand Down Expand Up @@ -164,31 +162,7 @@ export async function renderPage(options: SSROptions): Promise<Response> {
mod,
env,
});
const apiContext = createAPIContext({
request: options.request,
params: renderContext.params,
props: renderContext.props,
adapterName: options.env.adapterName,
});
if (options.middleware) {
if (options.middleware?.onRequest) {
const onRequest = options.middleware.onRequest as MiddlewareResponseHandler;
const response = await callMiddleware<Response>(env.logging, onRequest, apiContext, () => {
return coreRenderPage({
mod,
renderContext,
env: options.env,
cookies: apiContext.cookies,
});
});
const onRequest = options.middleware?.onRequest as MiddlewareResponseHandler | undefined;

return response;
}
}
return await coreRenderPage({
mod,
renderContext,
env: options.env,
cookies: apiContext.cookies,
}); // NOTE: without "await", errors won’t get caught below
return tryRenderPage(renderContext, env, mod, onRequest);
}
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { createRenderContext } from './context.js';
export type { RenderContext } from './context.js';
export { renderPage } from './core.js';
export { tryRenderPage } from './core.js';
export type { Environment } from './environment';
export { createEnvironment } from './environment.js';
export { getParamsAndProps } from './params-and-props.js';
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/vite-plugin-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function getPrivateEnv(
// Ignore public env var
if (envPrefixes.every((prefix) => !key.startsWith(prefix))) {
if (typeof process.env[key] !== 'undefined') {
const value = process.env[key];
let value = process.env[key];
// Replacements are always strings, so try to convert to strings here first
if (typeof value !== 'string') {
value = `${value}`;
}
// Boolean values should be inlined to support `export const prerender`
// We already know that these are NOT sensitive values, so inlining is safe
if (value === '0' || value === '1' || value === 'true' || value === 'false') {
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ describe('getStaticPaths - dev calls', () => {
await devServer.stop();
});

it('only calls getStaticPaths once', async () => {
it('only calls getStaticPaths once', async function () {
// Sometimes this fail in CI as the chokidar watcher triggers an update and invalidates the route cache,
// causing getStaticPaths to be called twice. Workaround this with 2 retries for now.
this.retries(2);

let res = await fixture.fetch('/a');
expect(res.status).to.equal(200);

Expand Down
Loading

0 comments on commit 873b401

Please sign in to comment.