Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
Absolute links within own domain in Gatsby

I would like to create an absolute link to files (images, downloads, ...) in my static directory, so during development the link would be http://localhost/myimage.jpg and once deployed it should be https://www.example.com/myimage.jpg.

Is there an easy way (e.g. an environment variable etc.) to reference the current domain name in Gatsby?

I would like to achieve this behavior without changing any config files.

A relative link is not an option in my case. The reason is that I am using the plugin gatsby-plugin-react-intl which modifies all relative links by adding the locale in front of it, meaning, the above mentioned link would automatically turn into https://www.example.com/en/myimage.jpg instead of https://www.example.com/myimage.jpg.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The default behavior is what you described. Using the static folder, a structure like /static/myimage.jpg will be referenced as https://somedomain.com/myimage.jpg.

The same approach applies to PDF or any static asset you wish to make available.

Following that, a structure like: /static/images/myimage.jpg will become https://somedomain.com/images/myimage.jpg.

Keep in mind that the static folder structure is "cloned" inside the public one once the site is built so everything inside will become public. /static becomes the root path once cloned.

I know that this is the default behavior. The problem is, that I'm using react-intl (or more specifically, gatsby-plugin-react-intl), which modifies all these paths by adding the locale in front of it, e.g. <img src='/myimage.jpg' /> becomes <img src='/en/myimage.jpg />. Using gatsby-image for all purposes is not possible, because it doesn't work with SVGs etc. I haven't figured out how to stop the plugin from doing this and I thought knowing how to refer to a your own domain in js (or react) would be something worth knowing.

Well, there are multiple ways to point to your current domain. From the window.location.origin (check caveats of using window or global objects in SSR) to the default location props exposed by Gatsby (because its extension from @reach/router, from React). Note that location props will be only available in top-level components (pages) but you can drill down or use it as you wish, as any other property.

In addition, the gatsby-plugin-react-intl plugin exposes some methods that provide the current language information, useful to build your own static path to the asset. Using:

  const { locale } = useIntl();

Will give you the current locale. You can use it to build your own asset like:

  <img src={`/${locale}/images/myimage.jpg`} />

This path will work whether you are in local development or in your hosted project domain, because of the relativity. If you still want to use the full domain, you can use the previously explained location (React-based approach) or the window.location (JavaScript-based approach).

Regarding the automatic modification of static assets and paths, I'm afraid it's the default plugin's behavior and I haven't found any way or exposed method to change it otherwise.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda