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

Is there any way to host SPA without local port? #2270

Closed
GiantappMan opened this issue Mar 15, 2022 · 5 comments
Closed

Is there any way to host SPA without local port? #2270

GiantappMan opened this issue Mar 15, 2022 · 5 comments
Assignees
Labels
feature request feature request

Comments

@GiantappMan
Copy link

GiantappMan commented Mar 15, 2022

for example, I built a SPA by NUXTJS.
I want to host it in webview2 but don't open any local ports.

I can do this in electron. is there any way to do the same thing in webview2, thanks

  mainWindow.loadURL(url.format({
    pathname: 'index.html',    /* Attention here: origin is path.join(__dirname, 'index.html') */
    protocol: 'file',
    slashes: true
  }))

app.on('ready', () => {
  protocol.interceptFileProtocol('file', (request, callback) => {
    const url = request.url.substr(7)    /* all urls start with 'file://' */
    let p = path.normalize(`${__dirname}/dist/${url}`);
    console.log("test", p);
    callback({ path: p })
  }, (err) => {
    if (err) console.error('Failed to register protocol')
  })
  createWindow()
})
@GiantappMan GiantappMan added the feature request feature request label Mar 15, 2022
@GiantappMan GiantappMan changed the title is there any way to host SPA without local port Is there any way to host SPA without local port? Mar 15, 2022
@champnic
Copy link
Member

Hey @DaZiYuan - Unfortunately I'm unfamiliar with "SPA" or "NUXTJS". Is there a specific capability you need from the WebView2? Can you highlight which part of the code you posted is not working or you need support for? Thanks!

@champnic champnic self-assigned this Mar 16, 2022
@GiantappMan
Copy link
Author

Hi @champnic,
here is a Single Page App, you can see it works correctly in localhost:5500.

image

but don't work in file protocol file:///C:/xx/Single-page-application/build/index.html
image

I just want to know if there is a way can host SPA in webview2 but don't open any local port just like the way in electron.

 protocol.interceptFileProtocol('file', (request, callback) => {
    const url = request.url.substr(7)    /* all urls start with 'file://' */
    let p = path.normalize(`${__dirname}/dist/${url}`);
    console.log("test", p);
    callback({ path: p })
  }, (err) => {
    if (err) console.error('Failed to register protocol')
  })

image

@champnic
Copy link
Member

Do you just want to load files from a location on disk? You can see if SetVirtualHostNameToFolderMapping works here:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.setvirtualhostnametofoldermapping?view=webview2-dotnet-1.0.1150.38

@GiantappMan
Copy link
Author

Thanks, works perfectly

        public MainWindow()
        {
            InitializeComponent();

            webview2.CoreWebView2InitializationCompleted += Webview2_CoreWebView2InitializationCompleted;
            webview2.Source = new Uri("https://appassets.example/index.html");
        }

        private void Webview2_CoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            webview2.CoreWebView2.SetVirtualHostNameToFolderMapping(
  "appassets.example", "build", CoreWebView2HostResourceAccessKind.DenyCors);
        }

@champnic
Copy link
Member

Glad you got it working! :)

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

No branches or pull requests

2 participants