Skip to content

Commit 089dd4d

Browse files
authored
Merge branch 'main' into codex/consolidate-pages-plugin
2 parents d007689 + 63b0d91 commit 089dd4d

40 files changed

Lines changed: 741 additions & 36 deletions

‎docs/guide/edge/redirects.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ Path-only sources apply to every domain on the project. Host-prefixed sources ap
5151

5252
The source must use `https://` and a literal hostname without wildcards or a port. Add the domain to your project with `void domain add` before deploying the rule. A domain awaiting certificate verification starts redirecting once it becomes active.
5353

54+
## HTTP to HTTPS
55+
56+
Set `routing.alwaysUseHttps` to redirect HTTP requests to HTTPS:
57+
58+
```ts
59+
import { defineConfig } from 'void/config';
60+
61+
export default defineConfig({
62+
routing: {
63+
alwaysUseHttps: true,
64+
},
65+
});
66+
```
67+
68+
Void answers with a permanent `308` redirect to the same URL over HTTPS. The path, query string, and request method stay the same. Omitted or `false` means off. `localhost` and loopback addresses such as `127.0.0.1` stay on HTTP, so local development and `vite preview` are unchanged.
69+
70+
It works on the Void platform and for Void apps on `void deploy --platform cloudflare`. It needs the Cloudflare target: setting it with `target: 'node'`, `'bun'`, or `'deno'` is a config error.
71+
72+
On `void deploy --platform cloudflare`, built files with a content hash in their name, such as `/assets/index-abc123.js`, can be served by Cloudflare before Void runs, so they may still answer over HTTP. Pages always redirect, so browsers load these files over HTTPS. To redirect every request, also turn on [Always Use HTTPS](https://developers.cloudflare.com/ssl/edge-certificates/additional-options/always-use-https/) for your Cloudflare zone.
73+
74+
SvelteKit, Nuxt, Astro, Analog, TanStack Start, React Router, and vinext apps run their own Worker, so `void deploy --platform cloudflare` stops with an error when the option is on. Static builds of these frameworks, deployed with `--dir` or `inference.appType`, redirect as usual. For the others, redirect to HTTPS in the framework, or turn on [Always Use HTTPS](https://developers.cloudflare.com/ssl/edge-certificates/additional-options/always-use-https/) for your Cloudflare zone.
75+
5476
## Framework `_redirects` files
5577

5678
Void reads `_redirects` files produced by supported frameworks. You can also place one in Vite's `publicDir`:

‎docs/reference/config.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ This choice applies to subsequent `void deploy` runs. Atomic deployment sends tr
366366

367367
Routing and edge configuration for headers, redirects, rewrites, and caching.
368368

369+
#### `routing.alwaysUseHttps`
370+
371+
Redirect HTTP requests to HTTPS with a permanent `308` redirect. Omitted or `false` means off. Requires the Cloudflare target. `localhost` stays on HTTP. See [HTTP to HTTPS](../guide/edge/redirects#http-to-https) for where it works.
372+
373+
```ts
374+
routing: {
375+
alwaysUseHttps: true,
376+
}
377+
```
378+
369379
#### `routing.headers`
370380

371381
Custom response headers. Keys are URL patterns, values are arrays of `"Name: value"` strings. See [Custom Headers](../guide/edge/headers) for details.

‎packages/deploy-cloudflare/src/static.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ export type CloudflareStaticDeployConfig = Readonly<{
2424
/** Generate the deterministic thin Worker entry bundled by Wrangler. */
2525
export function renderCloudflareStaticEntry(options: {
2626
runtimeImport?: string;
27+
alwaysUseHttps?: boolean;
2728
headerRules?: ReadonlyArray<HeaderRule>;
2829
redirectRules?: ReadonlyArray<RedirectRule>;
2930
fallbackRules?: ReadonlyArray<RedirectRule>;
3031
versionMetadataBinding?: string;
3132
}): string {
3233
const config = JSON.stringify({
34+
...(options.alwaysUseHttps !== undefined ? { alwaysUseHttps: options.alwaysUseHttps } : {}),
3335
...(options.headerRules?.length ? { headerRules: options.headerRules } : {}),
3436
...(options.redirectRules?.length ? { redirectRules: options.redirectRules } : {}),
3537
...(options.fallbackRules?.length ? { fallbackRules: options.fallbackRules } : {}),

‎packages/deploy-cloudflare/test/unit/static.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ import {
1010
} from '../../src/index.ts';
1111

1212
describe('Cloudflare static deployment', () => {
13+
it.each([true, false, undefined])(
14+
'preserves alwaysUseHttps %s in the generated entry',
15+
(alwaysUseHttps) => {
16+
const entry = renderCloudflareStaticEntry({ alwaysUseHttps });
17+
if (alwaysUseHttps === undefined) {
18+
expect(entry).not.toContain('alwaysUseHttps');
19+
} else {
20+
expect(entry).toContain(`"alwaysUseHttps":${alwaysUseHttps}`);
21+
}
22+
},
23+
);
1324
it('renders one deterministic shared-runtime entry', () => {
1425
expect(
1526
renderCloudflareStaticEntry({

‎packages/deploy-core/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ export type DeployManifest = {
278278
hashedAssetsPrefix?: string;
279279
headerRules?: Array<HeaderRule>;
280280
redirectRules?: Array<RedirectRule>;
281+
/** Omitted values inherit the platform default; preserve explicit false. */
282+
alwaysUseHttps?: boolean;
281283
fallbackRules?: Array<RedirectRule>;
282284
websocket?: WebSocketManifestMeta;
283285
sandbox?: SandboxManifestMeta;

‎packages/deploy-core/src/validate.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ export function validateManifestShape(manifest: unknown): string | null {
376376
}
377377
}
378378

379+
if (manifest.alwaysUseHttps !== undefined && typeof manifest.alwaysUseHttps !== 'boolean') {
380+
return 'invalid manifest shape: alwaysUseHttps must be a boolean';
381+
}
382+
379383
const redirectRulesError = validateRoutingRules(manifest.redirectRules, 'redirectRules');
380384
if (redirectRulesError) {
381385
return redirectRulesError;

‎packages/deploy-core/test/unit/validate.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { describe, expect, it } from 'vite-plus/test';
22
import { validateManifestShape, type DeployManifest } from '../../src/index.ts';
33

4+
describe('alwaysUseHttps', () => {
5+
it.each([true, false, undefined])('accepts %s', (alwaysUseHttps) => {
6+
expect(validateManifestShape({ version: 3, bindings: {}, alwaysUseHttps })).toBeNull();
7+
});
8+
it.each(['true', 1, null, {}])('rejects non-boolean %s', (alwaysUseHttps) => {
9+
expect(validateManifestShape({ version: 3, bindings: {}, alwaysUseHttps })).toContain(
10+
'alwaysUseHttps must be a boolean',
11+
);
12+
});
13+
});
14+
415
describe('custom Durable Object declarations', () => {
516
const binding = {
617
bindingName: 'WORKSPACE',

‎packages/edge/src/https.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/** Upgrade public HTTP requests without involving assets or application code. */
2+
export function redirectToHttps(request: Request): Response | undefined {
3+
const url = new URL(request.url);
4+
if (
5+
url.protocol !== 'http:' ||
6+
url.hostname === 'localhost' ||
7+
url.hostname.endsWith('.localhost') ||
8+
isIpv4Loopback(url.hostname) ||
9+
url.hostname === '[::1]' ||
10+
url.pathname.startsWith('/.well-known/acme-challenge/') ||
11+
url.pathname.startsWith('/.well-known/pki-validation/')
12+
) {
13+
return undefined;
14+
}
15+
url.protocol = 'https:';
16+
// Cloudflare's HTTP ports do not accept TLS. Upgrade to the default HTTPS port.
17+
url.port = '';
18+
return Response.redirect(url.href, 308);
19+
}
20+
21+
/**
22+
* `URL` already rewrites every IPv4 form (`127.1`, `0x7f.0.0.1`) to four
23+
* decimal parts and rejects parts above 255, so only the shape is checked.
24+
*/
25+
function isIpv4Loopback(hostname: string): boolean {
26+
const parts = hostname.split('.');
27+
return (
28+
parts.length === 4 &&
29+
parts[0] === '127' &&
30+
parts.every((part) => part !== '' && [...part].every((char) => char >= '0' && char <= '9'))
31+
);
32+
}

‎packages/edge/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type { ProjectZeroTrustRouting } from './access.ts';
2+
export { redirectToHttps } from './https.ts';
23
export {
34
applyHeaderRules,
45
parseClearSiteData,

‎packages/edge/src/runtime.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AssetConfig, HeaderRule, RedirectRule } from '@void/deploy-core';
22
import { applyHeaderRules } from './headers.ts';
3+
import { redirectToHttps } from './https.ts';
34
import { applyRoutingRules, mergeQueryIntoLocation } from './routing-rules.ts';
45

56
export type EdgeAssetFetcher = Readonly<{
@@ -11,6 +12,7 @@ export type StaticEdgeEnv = Readonly<{
1112
}>;
1213

1314
export type StaticEdgeConfig = Readonly<{
15+
alwaysUseHttps?: boolean;
1416
/** Optional native Worker self-metadata used to identify the served version. */
1517
versionMetadataBinding?: string;
1618
/**
@@ -50,6 +52,12 @@ export function createStaticEdgeHandler(config: StaticEdgeConfig): StaticEdgeHan
5052
{ headers: { 'cache-control': 'no-store' } },
5153
);
5254
}
55+
if (config.alwaysUseHttps) {
56+
const redirect = redirectToHttps(request);
57+
if (redirect) {
58+
return redirect;
59+
}
60+
}
5361
let effectiveUrl = new URL(originalUrl);
5462
let effectiveRequest = request;
5563

0 commit comments

Comments
 (0)