shelf icon indicating copy to clipboard operation
shelf copied to clipboard

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

Open 0x1af2aec8f957 opened this issue 1 year ago • 2 comments

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.

0x1af2aec8f957 avatar Dec 13 '23 09:12 0x1af2aec8f957

Would you explain a bit more?

kevmoo avatar Dec 13 '23 21:12 kevmoo

@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

0x1af2aec8f957 avatar Dec 18 '23 03:12 0x1af2aec8f957