skip to Main Content

Sorry its my first post so please bare with me.and if you could tell me what im doing wrong would be helpfull.
I have module with nested route.
I am running the built angular fles using nginx and it works fine with no error but every time i reload example.com/configuration/devices it gives blank page with 304 status and the error
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

But every other routes work fine when reload. Even exapmle.com/configuration works and redirects to /configuration/devices,

// app.module.ts
const routes: Routes = [
  { path: 'classroom', component: ClassroomComponent },
  {
    path: 'configuration',
    loadChildren: () =>
      import('./configuration/configuration.module').then(
        (m) => m.ConfigurationModule
      ),
  },
  {
    path: 'form',
    loadChildren: () => import('./form/form.module').then((m) => m.FormModule),
  },
  { path: '', redirectTo: '/classroom', pathMatch: 'full' },
];
// configuration-routing.modules.ts
const routes: Routes = [
  {
    path: '',
    component: ConfigurationComponent,
    children: [
      {
        path: 'logs',
        component: LogsComponent,
      },
      {
        path: 'devices',
        component: DevicesComponent,
      },
      {
        path: '',
        redirectTo: 'devices',
        pathMatch: 'full',
      },
    ],
  },
];

and nginx config as

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
   
    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  192.168.5.100;

       
        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        error_page  404              index.html;
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

And it works fine but when i refresh the page it gives returns 304 status and empty page
Error image
and cant find why its doing it.

Tried changing nginx config but dont know what to change

2

Answers


  1. Chosen as BEST ANSWER

    I removed --base-href ./ from my build command ng build application-local --base-href ./ and now it works but now i dont know why this work. Can anyone explain me this please


  2. My suggestion,
    when you declare –base-href ./ your application add "./" to URL when application load additional module (configuration.module). NGINX can’t find the JS file by "example.com/./configuration/devices" AND NGINX return html page with 404 error. But angular waited for JS, that’s why you get the error

    Failed to load module script: Expected a JavaScript module script but
    the server responded with a MIME type of "text/html". Strict MIME type
    checking is enforced for module scripts per HTML spec.

    Just change ./ to / (or skip the parameter)

    https://angular.io/api/common/APP_BASE_HREF

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search