Next JS 13 App router based not working on azure static web app

Tinasoa RAJAONARIVELO 25 Reputation points
2023-06-17T08:14:50.57+00:00

Hello,

I tried to follow this tutorial https://learn.microsoft.com/en-us/azure/static-web-apps/deploy-nextjs-hybrid but using App router based instead of Pages router and using next js 13.4.6.

The app is already live here : https://mango-coast-048dbb510.3.azurestaticapps.net/ . When I implement nested route sample like this "src/app/test/page.tsx" it works, but when using dynamic routing like adding "id" (src/app/test/[id]/page.tsx) or using routes groups, I got 500 error.

Then I tried to implement pages router based on the same project and it's fully work. You can test it with the following link : https://mango-coast-048dbb510.3.azurestaticapps.net/pages-test and https://mango-coast-048dbb510.3.azurestaticapps.net/pages-testdetail/1

NB : I set standalone mode on true.

Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
1,173 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Paula A 0 Reputation points
    2023-08-11T19:57:29.6866667+00:00

    Edit: I managed to get dynamic routes to work in Azure Static web app. You need to have two things in your project.

    1. Remember to add the function of generateStaticParams in the page.tsx of your dynamic route. It is a nextjs13 app router function to create the static bundle. More info here in Static Export for App Router documentation and in generateStaticParams documentation.
    2. Add staticwebapp.config.json file in the project root. More info in the Azure SWA documentation. (Really only you need the next code):
     "navigationFallback": {
        "rewrite": "index.html",
        "exclude": ["*.{css,scss,js,png,gif,ico,jpg,svg}"]
      },
    

    If you have pipeline:

    • Upload this file in the section pipeline/library
    • Update your pipeline to includes the artifact (Replace {BUILD} by your build name folder)
    // ... first steps of your pipeline 
    
    - task: DownloadSecureFile@1
      name: staticwebapp
      inputs:
        secureFile: 'staticwebapp.config.json'
      displayName: 'Obtain the staticwebapp file from the library'
    
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: 'cp $(staticwebapp.secureFilePath) {BUILD}/'
        workingDirectory: '$(System.DefaultWorkingDirectory)'
      displayName: 'Copy staticwebapp.config.json file to app location'
    
    // ... Here you code of connect your Azure SWA with your repo.
    
    • Authorize the security file one time when you run your pipeline (you will see the option after run the pipeline with an alert message).

    I hope to help somebody with this.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.