Skip to content
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

PathBase support for React UI #511

Closed
wants to merge 18 commits into from
Closed

PathBase support for React UI #511

wants to merge 18 commits into from

Conversation

daniel-lerch
Copy link
Contributor

@daniel-lerch daniel-lerch commented May 7, 2020

Summary of the changes

  • Use placeholders for PathBase in React source code
  • Implement a custom ISpaStaticFileProvider to replace these placeholders

Addresses #276
Fixes #504

This is an improved version of #388 by @szwenni

  • Better code style
  • Efficient caching with lazy reload
  • Registered in ConfigureHttpServices

Sven Kröger and others added 12 commits October 17, 2019 14:38
Uses in memory for all js files and index.html

Some code cleanup is still neccessary but it works frm first testings

For all other files that are not js files and not the index html PhysicalFileInfo is taken as fallback
Fixed Logger null error
…ptions.cs

Co-Authored-By: Loïc Sharma <sharma.loic@gmail.com>
…ly in production mode

commented reactdevserver back in whoch will be used in dev mode
@loic-sharma
Copy link
Owner

This looks great! Thank you for starting this. I left some comments but I need to dig deeper as I don't fully understand the static file providers just yet.

return new DirectoryContents(this, new DirectoryInfo(Path.Combine(_root.FullName, subpath.TrimStart('/'))));
}

public IFileInfo GetFileInfo(string subpath)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it if this feature was a little more "pay for play". Right now we always cache files in memory even the PathBase configuration wasn't set. It'd be better if we don't cache or replace __BAGET_PLACEHOLDER_... if PathBase isn't configured.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know three options to implement a custom pathbase with ASP.NET and Node:

  1. Replace __BAGET_PLACEHOLDER_... in every affected file and cache them. I would not expect any performance penalty as files are kept in memory. An instance of BaGet occupies ~100 MB RAM and caching all JavaScript files would another ~700 KB.
  2. Replace __BAGET_PLACEHOLDER_... only in index.html and implement code in config.tsx to dynamically replace __BAGET_PLACEHOLDER_... with an empty string. This would reduce the additional memory usage for standard deployments to less than 3 KB.
  3. Omit placeholders in index.html and use the concept from 2. for JavaScript. If a pathbase is configured we could edit index.html with regular expressions. This would require a full test coverage because we would have to rely on implementation details of the Node compiler.

If you know a better way to implement a custom pathbase please describe it to me or tell me which of these three options would like to have in BaGet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loic-sharma What's your opinion on that?

@loic-sharma
Copy link
Owner

I think we can simplify BaGetUIFileProvider by using the default ISpaStaticFileProvider's IFileProvider. I made a prototype of this here: https://github.com/loic-sharma/BaGet/compare/loshar-path-base

@daniel-lerch
Copy link
Contributor Author

I think we can simplify BaGetUIFileProvider by using the default ISpaStaticFileProvider's IFileProvider. I made a prototype of this here: https://github.com/loic-sharma/BaGet/compare/loshar-path-base

Yes, we can replace BaGetSpaStaticFileProvider with the default ISpaStaticFileProvider. I like the way you registered it in your prototype and will adapt that soon.

However, using the default IFileProvider which is actually a PhysicalFileProvider as a backend for our own file provider leads to a problem:
IFileProvider exposes a function GetDirectoryContents which contains an IEnumerable<IFileInfo> which are physical files and not the replaced and cached ones. It might be that ASP.NET Core only uses this method for directory indices which are not necessary for UI files but I don't like to rely on such implementation details.

Comment on lines +109 to +112
//if (env.IsDevelopment())
//{
// spa.UseReactDevelopmentServer(npmScript: "start");
//}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This React development server is really a headache for me. If it is enabled in all development environments, integration tests for my SPA file provider with custom path base fail. In addition to that we have a second middleware for serving SPA static files which should be disabled when using the React server.

My idea for a workaround would be to add a setting BaGetOptions.DisableReactDevelopmentServer. Per default, all development environments would use the React server and unit tests would disabled it with an in-memory configuration. Any other ideas?

@daniel-lerch
Copy link
Contributor Author

This repository has been inactive for too long to keep this PR up to date.

There are better ways to implement a custom base path for Vue.js which I found out working on another project. I am sure such an approach would work for BaGet and React, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Frontend doesn't use PathBase
3 participants