Skip to content

Add support for style tag nonce attributes #129

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ If no props are passed to `<NextTopLoader />`, below is the default configuratio
- `shadow`: a smooth shadow for the TopLoader. (set it to `false` to disable it)
- `template`: to include custom HTML attributes for the TopLoader.
- `zIndex`: defines zIndex for the TopLoader.
- `showAtBottom`: To show the TopLoader at bottom. (increase height for the TopLoader to ensure it's visibility at the mobile devices)
- `showForHashAnchor`: To show for "#" url or not. (set it to `false` to disable it)
- `showAtBottom`: to show the TopLoader at bottom. (increase height for the TopLoader to ensure it's visibility at the mobile devices).
- `showForHashAnchor`: to show for "#" url or not. (set it to `false` to disable it).
- `nonce`: to add nonces to the `<style>` tags, for [Content Security Policy](https://www.w3.org/TR/2016/REC-CSP2-20161215/#style-src-nonce-usage) (might require [skipping SSR](https://nextjs.org/docs/app/guides/lazy-loading#skipping-ssr) to avoid a [hydration error](https://nextjs.org/docs/messages/react-hydration-error)).

#### `NextTopLoaderProps` (props passed to the TopLoader)

Expand All @@ -221,6 +222,7 @@ If no props are passed to `<NextTopLoader />`, below is the default configuratio
| `zIndex` | `number` | `1600` |
| `showAtBottom` | `boolean` | `false` |
| `showForHashAnchor` | `boolean` | `true` |
| `nonce` | `string` | `undefined` |

## Contributors

Expand Down
9 changes: 7 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export type NextTopLoaderProps = {
*
*/
showForHashAnchor?: boolean;
/**
* Nonce of the inline styles for [Content Security Policy](https://www.w3.org/TR/2016/REC-CSP2-20161215/#script-src-the-nonce-attribute).
*/
nonce?: string;
};

/**
Expand All @@ -112,6 +116,7 @@ const NextTopLoader = ({
zIndex = 1600,
showAtBottom = false,
showForHashAnchor = true,
nonce,
}: NextTopLoaderProps): React.JSX.Element => {
const defaultColor = '#29d';
const defaultHeight = 3;
Expand All @@ -135,7 +140,7 @@ const NextTopLoader = ({
* CSS Styles for the NextTopLoader
*/
const styles = (
<style>
<style nonce={nonce}>
{`#nprogress{pointer-events:none}#nprogress .bar{background:${color};position:fixed;z-index:${zIndex};${positionStyle}left:0;width:100%;height:${height}px}#nprogress .peg{display:block;position:absolute;right:0;width:100px;height:100%;${boxShadow};opacity:1;-webkit-transform:rotate(3deg) translate(0px,-4px);-ms-transform:rotate(3deg) translate(0px,-4px);transform:rotate(3deg) translate(0px,-4px)}#nprogress .spinner{display:block;position:fixed;z-index:${zIndex};${spinnerPositionStyle}right:15px}#nprogress .spinner-icon{width:18px;height:18px;box-sizing:border-box;border:2px solid transparent;border-top-color:${color};border-left-color:${color};border-radius:50%;-webkit-animation:nprogress-spinner 400ms linear infinite;animation:nprogress-spinner 400ms linear infinite}.nprogress-custom-parent{overflow:hidden;position:relative}.nprogress-custom-parent #nprogress .bar,.nprogress-custom-parent #nprogress .spinner{position:absolute}@-webkit-keyframes nprogress-spinner{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(360deg)}}@keyframes nprogress-spinner{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}`}
</style>
);
Expand Down Expand Up @@ -245,7 +250,7 @@ const NextTopLoader = ({

// To debug the anchor target:
// console.log('Given target is', (anchor as HTMLAnchorElement).target);
const isExternalLink = ((anchor as HTMLAnchorElement).target as React.HTMLAttributeAnchorTarget) !== '' ;
const isExternalLink = ((anchor as HTMLAnchorElement).target as React.HTMLAttributeAnchorTarget) !== '';

// Check for Special Schemes
const isSpecialScheme = ['tel:', 'mailto:', 'sms:', 'blob:', 'download:'].some((scheme) =>
Expand Down
7 changes: 4 additions & 3 deletions src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const PagesTopLoader = ({
template,
zIndex = 1600,
showAtBottom = false,
nonce,
}: NextTopLoaderProps): JSX.Element => {
const defaultColor = '#29d';
const defaultHeight = 3;
Expand All @@ -34,8 +35,8 @@ export const PagesTopLoader = ({
!shadow && shadow !== undefined
? ''
: shadow
? `box-shadow:${shadow}`
: `box-shadow:0 0 10px ${color},0 0 5px ${color}`;
? `box-shadow:${shadow}`
: `box-shadow:0 0 10px ${color},0 0 5px ${color}`;

// Check if to show at bottom
const positionStyle = showAtBottom ? 'bottom: 0;' : 'top: 0;';
Expand All @@ -45,7 +46,7 @@ export const PagesTopLoader = ({
* CSS Styles for the NextTopLoader
*/
const styles = (
<style>
<style nonce={nonce}>
{`#nprogress{pointer-events:none}#nprogress .bar{background:${color};position:fixed;z-index:${zIndex};${positionStyle}left:0;width:100%;height:${height}px}#nprogress .peg{display:block;position:absolute;right:0;width:100px;height:100%;${boxShadow};opacity:1;-webkit-transform:rotate(3deg) translate(0px,-4px);-ms-transform:rotate(3deg) translate(0px,-4px);transform:rotate(3deg) translate(0px,-4px)}#nprogress .spinner{display:block;position:fixed;z-index:${zIndex};${spinnerPositionStyle}right:15px}#nprogress .spinner-icon{width:18px;height:18px;box-sizing:border-box;border:2px solid transparent;border-top-color:${color};border-left-color:${color};border-radius:50%;-webkit-animation:nprogress-spinner 400ms linear infinite;animation:nprogress-spinner 400ms linear infinite}.nprogress-custom-parent{overflow:hidden;position:relative}.nprogress-custom-parent #nprogress .bar,.nprogress-custom-parent #nprogress .spinner{position:absolute}@-webkit-keyframes nprogress-spinner{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(360deg)}}@keyframes nprogress-spinner{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}`}
</style>
);
Expand Down