dart-lang / shelf

Web server middleware for Dart
https://pub.dev/packages/shelf
BSD 3-Clause "New" or "Revised" License
893 stars 125 forks source link

Is it possible to add an allowFallback parameter to enable history mode? #397

Open 0x1af2aec8f957 opened 7 months ago

0x1af2aec8f957 commented 7 months ago

https://github.com/dart-lang/shelf/blob/b3adc7c5264b448a77427c6aacd67eedfb16dce2/pkgs/shelf_static/lib/src/static_handler.dart#L76

Is it possible to add an allowFallback parameter to enable history mode?

example: fileFound = _tryDefaultFile(allowFallback ? fileSystemPath : fsPath, defaultDocument);

Implement functionality similar to connect-history-api-fallback, which is important for integrating applications like Vue or React.

kevmoo commented 7 months ago

Would you explain a bit more?

0x1af2aec8f957 commented 6 months ago

@kevmoo Translated into the corresponding Nginx configuration:

location / {
  # root build_path;
  try_files $uri $uri/ /index.html;
}

Application scenario: When the requested file cannot be found, the top-level index.html file is always returned, which is important for providing web single-page applications.

Let me explain further through a practical scenario:

Now I have a static resources folder with the following structure

build_path
├── favicon.ico
├── index.html
├── static
   └── top_config.js

I hope that the mapping of the following paths can now be accessed through the createStaticHandler(build_path, defaultDocument: 'index.html') middleware:

Request path Mapping path
/ /index.html
/favicon.ico /favicon.ico
/static/top_config.js /static/top_config.js
/static /index.html
/static/other /index.html