-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bypass route protection on search-index build (server)? #210
Comments
Small update.. I have gone into the code of this lib and found out what exactly the problem is. When the Auth wrapper is present the SSR build consists of all pages with If I implement a check for "isBrowser" and disable the auth for the SSR, search works again as the The only possible solution here would be to provide some sort of optionial building of the |
Interesting case, I didn't know we can implement an auth guard with Docusaurus, since it's a static site generator. This plugin builds the search index right after Docusaurus built your pages, I don't know what's the output pages when you wrapped the root with an auth guard, if they contain Maybe you can build twice, one with auth enabled, another with auth disabled. Then move the |
@weareoutman haha yes. Bit of a hacky solution to be honest. Works better than I expected because of the way the SSR pages are built. Basically all pages, both SSR and CSR, are being rendered as I agree it won't be possible in one build. I think the easiest solution to at least enable search for this use case is making the That way I could for example build a small hook, that builds it locally, put the EDIT: you can probably close this issue, unless you think an optional build step is in the picture. Thanks anyway! |
I write such a code snippet and it builds the search index while I protect the site with Auth0: import useIsBrowser from "@docusaurus/useIsBrowser";
export default function Root({children}): JSX.Element {
const isBrowser = useIsBrowser();
return isBrowser ? (<Auth0Provider ...> ... </Auth0Provider>) : <>{children}</>;
} UPDATE: Not exactly. |
Replace #!/usr/bin/env bash
ROOT_DIR=$(git rev-parse --show-toplevel)
# shellcheck disable=SC2164
cd "$ROOT_DIR"
BUILD=1 yarn docusaurus build
mv build/search-index.json search-index.json
BUILD=0 yarn docusaurus build
mv search-index.json build/search-index.json export default function Root({children}): JSX.Element {
const {siteConfig} = useDocusaurusContext();
const {BUILD} = siteConfig.customFields;
return (BUILD > 0) ? <>{children}</> : (<Auth0Provider ...> ... </Auth0Provider>);
} |
Hi, I have the same problem when running in production. import React, { useEffect, useState} from 'react';
import { MsalProvider, AuthenticatedTemplate, UnauthenticatedTemplate, useMsal, useMsalAuthentication} from "@azure/msal-react";
import { PublicClientApplication, InteractionStatus, InteractionType} from "@azure/msal-browser";
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
// Default implementation, that you can customize
export default function Root({children}) {
const {siteConfig} = useDocusaurusContext();
const isProduction = process.env.NODE_ENV === 'production';
const redirectUri = isProduction ? `https://${siteConfig.organizationName}.github.io${siteConfig.baseUrl}index.html` : 'http://localhost:3000/';
const msalConfig = {
auth: {
clientId: "xxx",
authority: 'xxx',
redirectUri: redirectUri
},
cache: {
cacheLocation: 'localStorage',
storeAuthStateInCookie: false,
}
};
const pca = new PublicClientApplication(msalConfig);
const isLocalDevelopment = process.env.NODE_ENV === 'development';
const authProvider = isLocalDevelopment ? (
<main>
{children}
</main>
) : (
<MsalProvider instance={pca}>
<UnauthenticatedTemplate>
<Login/>
</UnauthenticatedTemplate>
<AuthenticatedTemplate>
<main>{children}</main>
</AuthenticatedTemplate>
</MsalProvider>
);
return <>{authProvider}</>;
}
function Login() {
const request = {
scopes: ["User.Read"],
prompt: 'select_account'
}
const { login, error } = useMsalAuthentication(InteractionType.Silent, request);
const msalInstance = useMsal();
useEffect(() => {
if (error && msalInstance.inProgress === InteractionStatus.None) {
login(InteractionType.Redirect, request);
}
}, [error, login, request, msalInstance]);
return null;
}; When I remove the Authentication components from the <MsalProvider instance={pca}>
<main>{children}</main>
</MsalProvider> Is there a way to manually add the Search bar component / force it? |
Hi!
Love the plugin/theme you have built here!
My Setup
Docusaurus tutorial page modified with a client side auth wrapper around the root element.
Problem
After running the
build
mysearch-index.json
file includes all routes/pages but is missing context information. This leads to always "no results" when searching:Description
I have somewhat of an edge case problem.. I need to protect the whole page via client side authentication. I'm aware that the content is only "hidden", which is sufficient for my usecase (no sensitive content).
What I have done to enable the authentication is:
theme/Root.js
to the standard docusaurus starter project{children}
in the auth context providerchildren
, or redirects to an authentication page if not authenticatedThis works as it should.
Now, when I build the project it generates the
search-index.json
, but the index is missing all the respective information for each page/md file and so forth (see above). Means auth works, but search doesn't.=> It seems like the local search crawler finds all of the pages, but can't access the content data because of the auth wrapper.
When I remove the auth wrapper the
search-index.json
is correctly populated with the content data and search works fine, but auth doesn't.Question
Is there any way to bypass the auth during the server build? Alternatively, maybe even a setting to turn off the auto generation of
search-index.json
on every build? This would mean I have to manually build thesearch-index.json
and put it into the static folder before every push, but I guess that's better than no search at all.I obviously prefer some way to bypass the auth during the build process for automation purposes.
Cheers
The text was updated successfully, but these errors were encountered: