Next.js: Difference between revisions

From David's Wiki
Line 10: Line 10:
davidl.me {
davidl.me {
   root * /var/www/davidl_me/out
   root * /var/www/davidl_me/out
  @pathWithSlash path_regexp dir ^(.*)/$
  redir @pathWithSlash {re.dir.1}
   try_files {path} {path}.html
   try_files {path} {path}.html
   file_server
   file_server
Line 17: Line 15:
</pre>
</pre>


* You can also use <code>uri strip_suffix /</code> instead of the redirect.
If you have folders with the same names as pages, you may want to remove trailing slashes:
<pre>
# Using a redirect
@pathWithSlash path_regexp dir ^(.*)/$
redir @pathWithSlash {re.dir.1}
# Using a rewrite
uri strip_suffix /
</pre>

Revision as of 19:32, 5 February 2023

Next.js allows you to deploy web applications with dynamic server-side and client-side rendering. It also supports static site generation which allows you to build a website and serve it from a file server of your choice.

Static HTML Export

Reference: https://nextjs.org/docs/advanced-features/static-html-export

With a static export, you can serve your website using any file server which can rewrite paths to html files. For example, in Caddy the following config would suffice:

davidl.me {
  root * /var/www/davidl_me/out
  try_files {path} {path}.html
  file_server
}

If you have folders with the same names as pages, you may want to remove trailing slashes:

# Using a redirect
@pathWithSlash path_regexp dir ^(.*)/$
redir @pathWithSlash {re.dir.1}
# Using a rewrite
uri strip_suffix /