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

Better Multi Spa with autostart #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Artem-Romanenia
Copy link

@Artem-Romanenia Artem-Romanenia commented Sep 18, 2023

Hello!

This PR adds support for automatic starting of dev servers when using multiple spas.

While implementing necessary changes, it only made sense to me to make some refactoring and change a design a little, otherwise it felt a little disparate.

Interface of the package remained the same, there are however four [Obsolete] methods (one does nothing now, and three others became just a 'proxy' to another methods).

Major changes are:

  • SpaProxyLaunchManager is made generic: SpaProxyLaunchManager<T> : IDisposable where T : SpaDevelopmentServerOptions which allows to register multiple instances with DI
  • SpaProxyMiddleware is also made generic, and is now responsible for forwarding requests to dev servers.
  • SpaDevelopmentServerOptions have their properties made virtual

For the scenario with multiple spa, there's now two options:

Old option with manual start:

app.MapSpaYarpForwarder("one", "https://localhost:44478"); //MapSpaYarpForwarder can work without middleware now
app.MapSpaYarpForwarder("two", "https://localhost:44479"); //MapSpaYarpForwarder can work without middleware now
// the old version was:
app.UseSpaYarpMiddleware(); //in this scenario this will be useless
app.MapSpaYarp("one", "https://localhost:44478"); //this one is just proxy to MapSpaYarpForwarder
app.MapSpaYarp("two", "https://localhost:44479"); //this one is just proxy to MapSpaYarpForwarder

Another option with autostart:

// This will autostart each dev server as developer opens respective spa pages.
app.MapSpaYarp<DevServerOptionsOne>(preserveMatchedPathSegment: true);
app.MapSpaYarp<DevServerOptionsTwo>(preserveMatchedPathSegment: true);

...


class DevServerOptionsOne : AspNetCore.SpaYarp.SpaDevelopmentServerOptions
{
    public override string WorkingDirectory => Path.Combine(base.WorkingDirectory, "ClientAppOne");
    public override string PublicPath => "one";
    public override string ClientUrl => "https://localhost:44478";
}

class DevServerOptionsTwo : AspNetCore.SpaYarp.SpaDevelopmentServerOptions
{
    public override string WorkingDirectory => Path.Combine(base.WorkingDirectory, "ClientAppTwo");
    public override string PublicPath => "two";
    public override string ClientUrl => "https://localhost:44479";
}

This approach potentially leaves a little bit of garbage in prod deployment in the form of DevServerOptionsOne and DevServerOptionsTwo which will not be used anywhere, I`m not sure if they can be optimized out in release build. Can be worked around with conditional compilation, though.

I also added 4th sample app for Multi Spa with autostart (90% of the changes in this PR), which is almost a copy of original multi spa sample with manual start, except for small details.

Please let me know what you think about this feature. If you have any suggestion on how this can be improved, I'd be glad to discuss and make necessary changes.

@berhir
Copy link
Owner

berhir commented Sep 29, 2023

Hi @Artem-Romanenia, thanks a lot for your time and effort to provide this PR! I am very interested in merging it, but it will take some time as I am currently busy with another project.

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.

2 participants