Dynamic routes in Next.js allow for creating pages with variable parts in the URL. This is useful for generating content-driven pages like blog posts. Dynamic routes are defined using files with brackets (e.g., [slug].js
) in the pages
directory. The dynamic parameter can be accessed using the useRouter
hook. Pages can be generated at build time or on request.
Static routes, on the other hand, have fixed URLs and separate files (e.g., about.js
) in the pages
directory. They are generated at build time and remain unchanged until the next build.
To deploy a Next.js application, follow these steps:
-
Build the Application: Run
npm run build
to create an optimized production build. -
Choose a Deployment Platform: Select a platform like Vercel, Netlify, or AWS Amplify.
-
Connect Repository: Connect the deployment platform to your Git repository.
-
Configure Build Settings: Set up build commands, environment variables, and other settings.
-
Deploy: Trigger deployment either manually or automatically on each commit.
Common deployment platforms include Vercel, Netlify, and AWS Amplify.
Next.js handles static file serving through the public
directory. Static assets like images or stylesheets are placed in this folder. They are served directly by the server and don't go through the Next.js routing.
To reference static assets:
- Images: Place them in
public/images
and use/images/image-name.jpg
in your components. - Stylesheets: Put them in
public/styles
and use/styles/style.css
in your components.
Using the /
prefix ensures that the assets are served from the root of your application.