Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuxtjs dynamic routes doesn't work on page reload after deploying as a SPA on AWS Amplify console

I have deployed my Nuxtjs app as SPA on AWS using AWS Amplify console. Now my website has some dynamic routes which redirects to 404 page when reloaded or opened in a new tab. I know that when we generate a static site using nuxt generate the routes should be using routes() in nuxt.config.js. But in SPA mode it should be working on page refresh or reload. Even in Angular when run in SPA mode dynamic routes work fine. Its so strange that the dynamic routes doesn't work when the website is run as a single page application.

When used locally in production mode i.e npm run build && npm run start the routes work fine. But after deploying it to AWS Amplify it redirects to 404 page. What am I misssing here? Here's the Amplify.yml config i used

version: 0.1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    # IMPORTANT - Please verify your build output directory
    baseDirectory: dist
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
test:
  artifacts:
    baseDirectory: cypress
    configFilePath: '**/mochawesome.json'
    files:
      - '**/*.png'
      - '**/*.mp4'
  phases:
like image 459
nick bing Avatar asked Nov 02 '25 07:11

nick bing


2 Answers

I wrote an article on how to deploy a Nuxt Application on Amplify here.

In a nutshell:

  1. Go to App Settings -> Rewrites and redirects
  2. Click on Edit and remove the existing rule
  3. Add the following new rule:

Source Address:
</^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf|map|json)$)([^.]+$)/>

Target Address:
/index.html

Type:
200 (Rewrite)

like image 168
Pascal Avatar answered Nov 04 '25 06:11

Pascal


@lupas helped me in nuxt discord. You just need to set as below

1) On the Amplify console go to: Rewrites and redirects 2) Delete the existing entry 3) Add the following: Source Address: Target address: /index.html Type: 200 (Rewrite)

like image 35
nick bing Avatar answered Nov 04 '25 06:11

nick bing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!