I am creating a telegram bot Web App and today I noticed that the routers stopped working. That is, they work on localhost:4200, but in the final build they do not work on localhost and netlify. Tell me what the problem is or told me what i doing wrong
https://web-tg-shop.netlify.app
if i click the button routes working if i write router in find line, routes not working..
// app.routes
export const routes: Routes = [
{path: '', loadComponent: () => import('./shop/components/welcome/welcome/welcome.component').then((c) => c.WelcomeComponent)},
{path: 'user', loadComponent: () => import('./shop/components/user/user/user.component').then((c) => c.UserComponent)},
{path: 'feedback', loadComponent: () => import('./shop/components/feedback/feedback.component').then(c => c.FeedbackComponent)}
];
all components are exists in directories
if i click the button or link – routes: /feedback and /user working correctly
if a write one of these url manually – all doesn’t work
Angular 16 routing stopped working
2
Answers
This problem is happening because your routes are not URLs that point to physical files. The server should be configured to serve the index.html file for all the traffic that comes to it.
From the documentation site:
So you will need to configure your web server and follow the examples mentioned here.
Since you mentioned that you are using Netlify, they published a guide for handling the routing. You will need to create a
_redirects
file with some configurations.Create a file called
_redirects
in thesrc
directory of your application with the following contentMake sure you include the
_redirects
file in yourangular.json
assets array so that Angular will include a copy of the file when building your project:In your
netlify.toml
file add the following:If this is due to an error with the publishing the below code might help. Not really sure what your problem is but this is how i got most of the build errors and 404 errors from not showing up anymore))
So in the working example you need to create _redirects file in the same level as package json. And add these lines
And then create netlify.toml file in the same level and add
Then within netlify site settings go to build and deploy and edit the publish directory from /dist to /dist/
To further clarify what i said, when i run npm run build on my localhost it it going to create a dist folder and within you will find another folder having the dist files . My folder name was web-frontend so i just added that to replace . You should run npm run build and get the folder name in dist and just add that folder name to the build and deploy settings pulish directory with /dist/
This should probably work check it out!