Skip to content

Serving Angular from Electron breaks since @angular/platform-browser 17.0.5 #53546

Closed

Description

@wartab

Which @angular/* package(s) are the source of the bug?

platform-browser, router

Is this a regression?

Yes

Description

Electron serves applications with the file:// protocol and therefore requires to set <base href=".">.

Unfortunately the commit that was supposed to just be a refactor breaks that use case.
162d940

As with a file system base path, these functions do not return the same value at all, which breaks the router.

Old relativePath implementation:
relativePath(".") --> '/C:/.../build/angular-builds/browser/en/'

New relativePath implementation:
relativePath(".") --> '/

I'd submit a PR to revert the changes, but according to @crisbeto's comment in that PR, this change was required for something in Material, so I assume something else needs to be done.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

As a consequence, you get a runtime error with nothing loading at all:

main-RYNXSWOY.js:162 ERROR Error: NG04002
    at fce.noMatchError (main-RYNXSWOY.js:1013:87185)
    at main-RYNXSWOY.js:1013:87870
    at main-RYNXSWOY.js:158:26148
    at See._error (main-RYNXSWOY.js:158:5647)
    at See.error (main-RYNXSWOY.js:158:2651)
    at See._error (main-RYNXSWOY.js:158:2897)
    at See.error (main-RYNXSWOY.js:158:2651)
    at See._error (main-RYNXSWOY.js:158:2897)
    at See.error (main-RYNXSWOY.js:158:2651)
    at See._error (main-RYNXSWOY.js:158:2897)

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 17.0.6
Node: 20.10.0
Package Manager: npm 9.6.4
OS: win32 x64

Angular: 17.0.6
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1700.6
@angular-devkit/build-angular   17.0.6
@angular-devkit/core            17.0.6
@angular-devkit/schematics      17.0.6
@angular/cdk                    17.0.3
@angular/material               17.0.3
@schematics/angular             17.0.6
rxjs                            7.8.1
typescript                      5.3.3
zone.js                         0.14.2

Anything else?

No response

Activity

  1. crisbeto commented on Dec 13, 2023

    @crisbeto
    Member

    Are you sure that the base element is pointing to "."? I just tried both the new and old implementations of relativePath('.') and they return the same value. I'm wondering if Electron isn't patching the URL class somehow.

  2. wartab commented on Dec 13, 2023

    @wartab
    ContributorAuthor

    Yes, it is. Try the following (make sure you serve it as a file, not from an HTTP server):

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
    <head>
        <meta charset="UTF-8">
        <title>Test</title>
        <base href=".">
    </head>
    <body>
    <script>
        let urlParsingNode;
        function oldRelativePath(url) {
            urlParsingNode = urlParsingNode || document.createElement('a');
            urlParsingNode.setAttribute('href', url);
            const pathName = urlParsingNode.pathname;
            return pathName.charAt(0) === '/' ? pathName : `/${pathName}`;
        }
    
        function newRelativePath(url) {
            return new URL(url, 'http://a').pathname;
        }
    
        document.body.innerHTML += oldRelativePath(".") + "<br>";
        document.body.innerHTML += newRelativePath(".");
    
    </script>
    </body>
    </html>

    You'll see

    /C:/Users/Vincent/test/
    / 
    
  3. crisbeto commented on Dec 13, 2023

    @crisbeto
    Member

    Thanks for the repro. I need to look into it more, but I think that we can resolve it by passing in document.baseURI instead of the http://a.

  4. wartab commented on Dec 13, 2023

    @wartab
    ContributorAuthor

    I came to a similar conclusion. I'm using location.href however, not sure if it makes a difference.

  5. scottg521 commented on Dec 13, 2023

    @scottg521

    I was just about to add what I think is the same issue. We are using './' as the base-href in our builds, and if the app is served from somewhere below root, we get the entire path from root in the activated route, and the router doesn't work with parameters. I have repos that illustrate the problem (which I can provide somehow if it would help), but here is what they produce:

    compare

    Here are the routes:

    export const routes: Routes = [
      { path: '404', component: PageNotFoundComponent },
      { path: '', redirectTo: '404', pathMatch: 'full' },
      { path: ':page', component: HeroesListComponent },
      { path: '**', component: PageNotFoundComponent }
    ];
  6. added a commit that references this issue on Dec 13, 2023
    d5c631b
  7. wartab commented on Dec 14, 2023

    @wartab
    ContributorAuthor

    @scottg521 Can you try to update to Angular 17.0.7? The issue I mentioned has been addressed and I assume so is yours.

  8. scottg521 commented on Dec 15, 2023

    @scottg521

    Yes, confirming that 17.0.7 fixes our issue. Thanks for the quick response.

  9. biondiBagasta commented on Dec 28, 2023

    @biondiBagasta

    @scottg521 Can you try to update to Angular 17.0.7? The issue I mentioned has been addressed and I assume so is yours.

    Hey did u fixed it??? I got this error while running electron on my Angular 17 :

    chunk-DVUE4UAR.js:7 ERROR Error: NG04002
        at _s.noMatchError (chunk-TM6YNNTS.js:2:100971)
        at chunk-TM6YNNTS.js:2:101646
        at chunk-DVUE4UAR.js:3:13351
        at Jo._error (chunk-DVUE4UAR.js:3:12151)
        at Jo.error (chunk-DVUE4UAR.js:3:2591)
        at Jo._error (chunk-DVUE4UAR.js:3:2831)
        at Jo.error (chunk-DVUE4UAR.js:3:2591)
        at Jo._error (chunk-DVUE4UAR.js:3:2831)
        at Jo.error (chunk-DVUE4UAR.js:3:2591)
        at Jo._error (chunk-DVUE4UAR.js:3:2831)
    
  10. added a commit that references this issue on Jan 23, 2024
    44c0cc2
  11. added a commit that references this issue on Jan 26, 2024
    04d861f
  12. added a commit that references this issue on Jan 26, 2024
    8dcb5c0
  13. angular-automatic-lock-bot commented on Jan 28, 2024

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions