Gatsby: docs: add info about location.href

Created on 4 Mar 2020  路  10Comments  路  Source: gatsbyjs/gatsby

Summary

  • add section about location.href is not available in SSR

Motivation

some mistakes are coming from using location.href in server side rendering - but there is no place in docs where is a hint about this

-const href = location.href
+const href = `${siteUrl}${pathname}`

References

https://css-tricks.com/how-to-the-get-current-page-url-in-gatsby/#article-header-id-4

Steps to resolve this issue

Docs

maybe placing infos here:

Draft the doc

Open a pull request

  • [ ] Open a pull request with your work including the words "closes #[this issue's number]" in the pull request description
help wanted documentation

Most helpful comment

On top of documention, we can make trying to access location.href to throw error in code (which would contain link to appropriate docs. Alternatively we could maybe construct .href for SSR? But I feel like constructing it ourselves might just hide some problems and introduce even more problems (subtle, and hard to debug)

All 10 comments

On top of documention, we can make trying to access location.href to throw error in code (which would contain link to appropriate docs. Alternatively we could maybe construct .href for SSR? But I feel like constructing it ourselves might just hide some problems and introduce even more problems (subtle, and hard to debug)

Also ran into this, using location.href for meta tags, which have to be SSRd. I think Gatsby should:

  • Document what's available on location in SSR/buildtime
  • Error if trying to access location.href when it's not available
  • Document using location.pathname and the site's canonical URL instead

I don't think Gatsby should try and construct location.href in SSR envs. It's pretty trivial to do yourself, and far less opaque. Just needs to be documented, and not silently fail.

Can i work on this documentation ?

Sure @imrishabh18, we would love your contribution!

@marcysutton Hey, can you please help me what should i write in the docs ?

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Not stale, still needs a fix AFAIK

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Still not stale

To provide an error message in this situation, there needs to be a change to reach/router. I've submitted an issue and a PR on reach that should fix it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ferMartz picture ferMartz  路  3Comments

totsteps picture totsteps  路  3Comments

KyleAMathews picture KyleAMathews  路  3Comments

theduke picture theduke  路  3Comments

rossPatton picture rossPatton  路  3Comments