Skip to content

Angular 17 SSR baseHref / baseUrl is not respected #27356

Closed

Description

@JustDoItSascha

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

platform-server

Is this a regression?

Yes

Description

Maybe I'm stupid or don't see something, but I think there is something wrong. I want to deploy an angular ssr app under a subpath of a domain, for example https://example.org/myapp. This subpath is handled by an nginx reverse proxy. So far so good.

Now I'm adding baseHref to the angular.json. It gets added to the index.html, everything fine. But if I'm looking to the server.ts I see the following:

server.get('*.*', express.static(browserDistFolder, {
        maxAge: '1y',
    }));

server.get('*', (req, res, next) => {
        const { protocol, originalUrl, baseUrl, headers } = req;

        commonEngine
            .render({
                bootstrap,
                documentFilePath: indexHtml,
                url: `${protocol}://${headers.host}${originalUrl}`,
                publicPath: browserDistFolder,
                providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
            })
            .then((html) => res.send(html))
            .catch((err) => next(err));
    });

But the baseUrl is always empty. Because the baseUrl can only be set if the code would look like this:

const router = express.Router();

router.get('*.*', express.static(browserDistFolder, {
        maxAge: '1y',
    }));

router.get('*', (req, res, next) => {
        const { protocol, originalUrl, baseUrl, headers } = req;

        commonEngine
            .render({
                bootstrap,
                documentFilePath: indexHtml,
                url: `${protocol}://${headers.host}${originalUrl}`,
                publicPath: browserDistFolder,
                providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
            })
            .then((html) => res.send(html))
            .catch((err) => next(err));
    });

server.use('/myapp', router);

Without using .use() the baseUrl MUST be empty. So I don't understand the whole purpose behind the default code? And of course it's not working with the default code, otherwise I wouldn't write.

So it looks like this scenario is not possible without changing the server.ts fundamentally. And it would be nice, if this changes are described in the docs.

Or if they were the default and changes to the baseHref option in the angular.json would also affect the server.ts. That I don't have to change the server.use() part manually.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

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

Angular 17.3.1

Anything else?

No response

Activity

  1. alfaex commented on Apr 16, 2024

    @alfaex
  2. hiepxanh commented on May 2, 2024

    @hiepxanh
  3. everflux commented on Jul 28, 2024

    @everflux
  4. self-assigned this
    on Aug 30, 2024
  5. added 19 commits that reference this issue on Sep 17, 2024
    3814f19
    6219e18
    abd4654
    602007d
    b8c2c09
    5c30f3d
    23c32aa
    f582e7e
    f6ccb97
    1163903
    598016d
    8e1a54c
    d33ec81
    293e376
    4046b77
    655b76a
    3974957
    f01d2da
    5de4d93
  6. added a commit that references this issue on Sep 19, 2024
    3b00fc9
  7. angular-automatic-lock-bot commented on Oct 20, 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.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions