loading...

Learning NextJs - Day 9

eperedo profile image Eduardo P. Rivero Updated on ・1 min read

Learning NextJs (12 Part Series)

1) Learning NextJs - Day 1 2) Learning NextJs - Day 2 3 ... 10 3) Learning NextJs - Day 3 4) Learning NextJs - Day 4 5) Learning NextJs - Day 5 6) Learning NextJs - Day 6 7) Learning NextJs - Day 7 8) Learning NextJs - Day 8 9) Learning NextJs - Day 9 10) Learning NextJs 9! - Day 10 11) Learning NextJs - Day 11 12) Easy https server with nextjs and ngrok

NextJs by default allows you to bring your own static files (js, css, images, json, etc) without any extra configuration.
In order to do that you need an "static" folder inside your project.

So let's create the folder with that specific name

mkdir static

Now you can put an image there (or any other static file) and use it in your pages. Let's pretend you have the logo of your app inside the static under the creative name of mylogo.png.

You can use the following path to show the logo in an img tag

/static/mylogo.png

Let's create a contact.js page

function Contact() {
  return (
    <img src="/static/mylogo.png" alt="My App Name" />
  );
}

export default Contact

And if you start your NextJs server and go to the url /contact you will see your logo.

Custom Server

If you have integrated next with express, hapijs or any other web framework, you will need to declare that route on your web framework as well.

If you are using hapijs the route will look like this

// other code omited 
// for reading purposes

server.route({
  method: 'GET',
  path: '/static/{p*}',
  handler: nextHandlerWrapper(app),
});

With this you are telling HapiJs to use the nextHandlerWrapper every time a request to the static folder is made.

Learning NextJs (12 Part Series)

1) Learning NextJs - Day 1 2) Learning NextJs - Day 2 3 ... 10 3) Learning NextJs - Day 3 4) Learning NextJs - Day 4 5) Learning NextJs - Day 5 6) Learning NextJs - Day 6 7) Learning NextJs - Day 7 8) Learning NextJs - Day 8 9) Learning NextJs - Day 9 10) Learning NextJs 9! - Day 10 11) Learning NextJs - Day 11 12) Easy https server with nextjs and ngrok

Posted on by:

eperedo profile

Eduardo P. Rivero

@eperedo

Javascript developer, 900 days duolingo streak

Discussion

markdown guide