-
Notifications
You must be signed in to change notification settings - Fork 87
feat(serve): Add --spa-fallback flag for SPA routing #2656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9fdd02e
759d7f5
ebac34b
f3030f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,6 +195,43 @@ class WebDevServer { | |
cascade = cascade.add(assetHandler); | ||
} | ||
|
||
if (options.configuration.spaFallback) { | ||
FutureOr<Response> spaFallbackHandler(Request request) async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of defining this inline, maybe this could be defined in a separate file, similar to That could make it a little bit easier to add a test for! |
||
final hasExt = request.url.pathSegments.isNotEmpty && | ||
request.url.pathSegments.last.contains('.'); | ||
Comment on lines
+200
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do we see here if the |
||
if (request.method != 'GET' || hasExt) { | ||
return Response.notFound('Not Found'); | ||
} | ||
|
||
final indexUri = | ||
request.requestedUri.replace(path: 'index.html', query: ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised that |
||
|
||
final cleanHeaders = Map.of(request.headers) | ||
..remove('if-none-match') | ||
..remove('if-modified-since'); | ||
|
||
final proxiedReq = Request( | ||
'GET', | ||
indexUri, | ||
headers: cleanHeaders, | ||
context: request.context, | ||
protocolVersion: request.protocolVersion, | ||
); | ||
|
||
final resp = await assetHandler(proxiedReq); | ||
|
||
if (resp.statusCode != 200 && resp.statusCode != 304) { | ||
return Response.notFound('Not Found'); | ||
} | ||
return resp.change(headers: { | ||
...resp.headers, | ||
'content-type': 'text/html; charset=utf-8', | ||
}); | ||
} | ||
|
||
cascade = cascade.add(spaFallbackHandler); | ||
} | ||
|
||
final hostname = options.configuration.hostname; | ||
final tlsCertChain = options.configuration.tlsCertChain ?? ''; | ||
final tlsCertKey = options.configuration.tlsCertKey ?? ''; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any interest on more thoroughly documenting this param here? Is there a better place to describe this? (it seems that lots of parameters are missing)