Closed phwt closed 11 months ago
As mentioned here https://docusaurus.io/docs/advanced/ssg#useisbrowser useIsBrowser
will become true only after first render.
Alternatively you can usetypeof window !== 'undefined'
const isBuildStage = typeof window !== 'undefined' && process.env.npm_lifecycle_event === "build"
Or you can also just use process env alone with optional chanining
// This will work fine in browser as well
const isBuildStage = process?.env?.npm_lifecycle_event === "build"
Thanks for pointing out! Looks like this is a Docusaurus related issue rather than the plugin itself.
Will update and close the issue after I can get auth working with this great plugin.
Also, I was wondering is it possible to make auth work without conditionally rendering the root?
Or the content must always be visible (returned) for the indexing to work?
Indexing happens during build process, so yes you need to have some condition
Hello, I've implemented an authentication that requires wrapping the root component with the authentication provider and checking access before displaying the content as shown below:
/src/theme/Root.tsx
/src/components/App.tsx
Basically, it will check if a user is authenticated before displaying the content, if not the user will be redirected to complete the sign-in first.
The authentication works fine but now the documents aren't being indexed.
After further investigation, I found that because in the
App.tsx
theisAuthenticated
will always befalse
during build and indexing which will result inchildren
not being returned and the page content cannot be indexed.Workaround
A workaround is to render the
children
during the build-time where the contents will be indexed. With the help ofisBrowser
andnpm_lifecycle_event
. I was able to make the indexing work while having the authentication check as in the code shown below:But this is a workaround and not a solution as I found that the
isBuildStage
istrue
when the page is first loaded and laterfalse
as intended. This makes the site's content briefly visible to the user before they are redirected. (I don't know whyisBrowser
isfalse
andprocess
is not undefined on the first load).Hope anyone can help me with this, thanks!