analog icon indicating copy to clipboard operation
analog copied to clipboard

bug: baseHref other than root is ignored by server side fetching routes

Open leblancmeneses opened this issue 5 months ago • 0 comments

Please provide the environment you discovered this bug in.

    "@nx/angular": "18.1.2",
    "@analogjs/platform": "^1.0.0-rc.0",

Which area/package is the issue in?

router

Description

When adding a baseUrl server-side fetching routes: (index).server.ts api cannot be reached through the specified VITE_ANALOG_PUBLIC_BASE_URL pathname. Given:

VITE_ANALOG_PUBLIC_BASE_URL="http://localhost:3000/startupwiki/"

It still goes to /api not /startupwiki/api

startupwiki is lost in this line: https://github.com/analogjs/analog/blob/532e29984db78c465f98ca08bf13ba91097a47d3/packages/router/src/lib/route-config.ts#L42

perhaps:

        const url = new URL('', import.meta.env['VITE_ANALOG_PUBLIC_BASE_URL']);
        url.pathname = `${url.pathname.endsWith('/')? url.pathname : url.pathname + '/'}api/_analog${routeConfig2[ANALOG_META_KEY].endpoint}`;

but then we need to fix the route registration toRoutes

======= Reproduce

Anyone else wanting to put an analog.js site behind a load balancer based on a route prefix.

npx nx run startupwiki:build:production --baseHref='/startupwiki/' --skip-nx-cache

where --baseHref='/startupwiki/' prefixes assets correctly on index.html e.g.

    <link rel="icon" type="image/x-icon" href="/startupwiki/assets/favicon-B-QwvwhC.ico" />
    <script type="module" crossorigin src="/startupwiki/assets/index-BeIyF34o.js"></script>

NITRO_APP_BASE_URL="/startupwiki/" prefixes the api http://localhost:3000/startupwiki/api/v1/hello where vite.config.ts isanalog({ apiPrefix: 'api', as documented here: https://analogjs.org/docs/features/api/overview#dynamic-api-routes

Please provide the exception or error you saw

ur {headers: dt, status: 404, statusText: 'Cannot find any path matching /api/_analog/pages/-index-.', url: 'http://localhost:3000/api/_analog/pages/-index-', ok: false, …}
error
: 
{url: '/api/_analog/pages/-index-', statusCode: 404, statusMessage: 'Cannot find any path matching /api/_analog/pages/-index-.', message: 'Cannot find any path matching /api/_analog/pages/-index-.'}
headers
: 
dt {normalizedNames: Map(5), lazyUpdate: null, headers: Map(5)}
message
: 
"Http failure response for http://localhost:3000/api/_analog/pages/-index-: 404 Cannot find any path matching /api/_analog/pages/-index-."
name
: 
"HttpErrorResponse"
ok
: 
false
status
: 
404
statusText
: 
"Cannot find any path matching /api/_analog/pages/-index-."
url
: 
"http://localhost:3000/api/_analog/pages/-index-"
[[Prototype]]
: 
_c


### Other information

It should be looking for ```/startupwiki/api/_analog/pages/-index-```

### I would be willing to submit a PR to fix this issue

- [ ] Yes
- [X] No

leblancmeneses avatar Mar 22 '24 03:03 leblancmeneses