Serve Static Files
Plumier provided functionality to serve static file using koa-send middleware
Enable Functionality#
Serve static files is optional, you can enable this functionality by install @plumier/serve-static and plug ServeStaticFacility into Plumier application
If no root directory provided it will automatically search for directory named www in the same level with the application.
You can serve multiple folder by specifying multiple facility
Serve Single File From Controller#
Its possible to serve single file from controller by using FileActionResult or response.file()
History Api Fallback#
History Api Fallback help SPA application which commonly handle navigation using History Api. Its become problem when user hitting refresh button in the middle of navigation or if user bookmark the page because it will result in 404.
Plumier provide @route.historyApiFallback() decorator to automatically redirect all the browser request (API or file request can be detected) into the specific controller action. Internally it detects the request Accept header to guess if the request comes from a browser or an API call.
info
Keep in mind that @route.historyApiFallback() require the ServeStaticFacility enabled on the Plumier application.
Example above showing that you host the SPA html built by create-react-app or @vue/cli as the root url /. The @route.historyApiFallback() will automatically redirect all browser request into the root url / with exception:
- Requested url doesn't have any appropriate controller / handler
- Requested url is not a static file serve
- Requested url is not an API call
