Skip to content

Commit 7eb494f

Browse files
committed
Try to fix up WIC routing
Previously this worked fine in dev, but couldn't start the first step in production. Still slightly unclear why, but this seems triggered by the Caddy redirect, so I'm guessing some kind of prefetch or something? Very weird but whatever.
1 parent 20599f1 commit 7eb494f

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

Caddyfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
{$DOMAIN:http://localhost} {
2-
# Will-it-cors subpaths are SPA-only, fresh loads must go back to the start:
3-
@will-it-cors-step path_regexp ^/will-it-cors/.+
4-
redir @will-it-cors-step /will-it-cors/ 308
5-
62
# Redirect various old URL that have been now replaced:
73
redir /toolkit / 308
84
redir /toolkit/ / 308

src/app/will-it-cors/components/steps/index.tsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,39 @@ const steps = new WillItCorsStore();
3030
export const Steps = observer(({ currentStep }: { currentStep: WillItCorsSteps }) => {
3131
const router = useRouter();
3232

33+
// If you end up directly on a step without having completed the path this session (e.g.
34+
// if you refresh the page or navigate there directly), we redirect you to the root.
35+
if (currentStep !== 'source-url' && !steps.sourceUrl) {
36+
router.push('/will-it-cors/');
37+
return null;
38+
}
39+
3340
switch (currentStep) {
3441
case 'source-url':
3542
return (
3643
<SourceUrlQuestion
37-
value={steps.sourceUrl}
44+
value={steps.sourceUrl || 'https://'}
3845
onChange={newValue => {
3946
steps.setSourceUrl(newValue);
4047
}}
41-
onNext={() => router.push('/will-it-cors/target-url')}
48+
onNext={() => router.push('/will-it-cors/target-url/')}
4249
/>
4350
);
4451

4552
case 'target-url':
4653
return (
4754
<TargetUrlQuestion
48-
value={steps.targetUrl}
55+
value={steps.targetUrl || 'https://'}
4956
onChange={newValue => {
5057
steps.setTargetUrl(newValue);
5158
}}
5259
onNext={() => {
5360
if (!steps.isCorsRequest) {
54-
router.push('/will-it-cors/not-cors');
61+
router.push('/will-it-cors/not-cors/');
5562
} else if (steps.isMixedContentRequest) {
56-
router.push('/will-it-cors/mixed-content');
63+
router.push('/will-it-cors/mixed-content/');
5764
} else {
58-
router.push('/will-it-cors/method');
65+
router.push('/will-it-cors/method/');
5966
}
6067
}}
6168
/>
@@ -79,7 +86,7 @@ export const Steps = observer(({ currentStep }: { currentStep: WillItCorsSteps }
7986
onChange={newValue => {
8087
steps.setMethod(newValue);
8188
}}
82-
onNext={() => router.push('/will-it-cors/request-extras')}
89+
onNext={() => router.push('/will-it-cors/request-extras/')}
8390
sourceOrigin={steps.sourceOrigin ?? 'https://'}
8491
targetOrigin={steps.targetOrigin ?? 'http://'}
8592
/>
@@ -102,11 +109,11 @@ export const Steps = observer(({ currentStep }: { currentStep: WillItCorsSteps }
102109
}}
103110
onNext={() => {
104111
if (steps.method === 'POST' && steps.contentType === undefined) {
105-
router.push('/will-it-cors/content-type');
112+
router.push('/will-it-cors/content-type/');
106113
} else if (steps.isSimpleCorsRequest) {
107-
router.push('/will-it-cors/simple-cors');
114+
router.push('/will-it-cors/simple-cors/');
108115
} else {
109-
router.push('/will-it-cors/preflight');
116+
router.push('/will-it-cors/preflight/');
110117
}
111118
}}
112119
/>
@@ -125,16 +132,16 @@ export const Steps = observer(({ currentStep }: { currentStep: WillItCorsSteps }
125132
}}
126133
onNext={() => {
127134
if (steps.isSimpleCorsRequest) {
128-
router.push('/will-it-cors/simple-cors');
135+
router.push('/will-it-cors/simple-cors/');
129136
} else {
130-
router.push('/will-it-cors/preflight');
137+
router.push('/will-it-cors/preflight/');
131138
}
132139
}}
133140
/>
134141
);
135142

136143
case 'simple-cors':
137-
return <SimpleCorsRequest onNext={() => router.push('/will-it-cors/server-response')} />;
144+
return <SimpleCorsRequest onNext={() => router.push('/will-it-cors/server-response/')} />;
138145

139146
case 'server-response':
140147
return (
@@ -151,9 +158,9 @@ export const Steps = observer(({ currentStep }: { currentStep: WillItCorsSteps }
151158
}}
152159
onNext={() => {
153160
if (steps.isServerResponseReadable) {
154-
router.push('/will-it-cors/request-success');
161+
router.push('/will-it-cors/request-success/');
155162
} else {
156-
router.push('/will-it-cors/request-failure');
163+
router.push('/will-it-cors/request-failure/');
157164
}
158165
}}
159166
/>
@@ -174,18 +181,18 @@ export const Steps = observer(({ currentStep }: { currentStep: WillItCorsSteps }
174181
sourceOrigin={steps.sourceOrigin ?? 'https://'}
175182
responseHeaders={steps.serverResponseHeaders}
176183
sendCredentials={steps.sendCredentials}
177-
onNext={() => router.push('/will-it-cors/show-code')}
184+
onNext={() => router.push('/will-it-cors/show-code/')}
178185
/>
179186
);
180187

181188
case 'show-code':
182189
return <ShowCode code={steps.exampleCode} />;
183190

184191
case 'preflight':
185-
return <PreflightRequest onNext={() => router.push('/will-it-cors/preflight-response')} />;
192+
return <PreflightRequest onNext={() => router.push('/will-it-cors/preflight-response/')} />;
186193

187194
case 'preflight-success':
188-
return <ServerAllowsPreflightRequest onNext={() => router.push('/will-it-cors/server-response')} />;
195+
return <ServerAllowsPreflightRequest onNext={() => router.push('/will-it-cors/server-response/')} />;
189196

190197
case 'preflight-response':
191198
return (
@@ -202,9 +209,9 @@ export const Steps = observer(({ currentStep }: { currentStep: WillItCorsSteps }
202209
}}
203210
onNext={() => {
204211
if (steps.isPreflightSuccessful) {
205-
router.push('/will-it-cors/preflight-success');
212+
router.push('/will-it-cors/preflight-success/');
206213
} else {
207-
router.push('/will-it-cors/preflight-failure');
214+
router.push('/will-it-cors/preflight-failure/');
208215
}
209216
}}
210217
/>

src/app/will-it-cors/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function WillItCorsPage() {
3434
}
3535
withDownload={false}
3636
cta={{
37-
href: '/will-it-cors/source-url',
37+
href: '/will-it-cors/source-url/',
3838
title: 'Get Started!',
3939
$variant: 'primary',
4040
$withBorder: true,

src/app/will-it-cors/store/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ function isSafeContentType(contentType: string) {
1818
const UNSAFE_HEADER_BYTES = '"():<>?@[\\]{}'.split('');
1919

2020
export class WillItCorsStore {
21-
// The various props. They each start as undefined, become empty values (""/{}/[])
22-
// when the question is ready, and then get updated with input.
23-
sourceUrl = 'https://';
24-
targetUrl = 'https://';
21+
sourceUrl = '';
22+
targetUrl = '';
2523
method = '';
2624

2725
sendCredentials = false;

0 commit comments

Comments
 (0)