Skip to content

Commit b2465a1

Browse files
committed
Add support for style tag nonce attributes (fixes TheSGJ#128)
1 parent cb8c35d commit b2465a1

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ If no props are passed to `<NextTopLoader />`, below is the default configuratio
201201
- `shadow`: a smooth shadow for the TopLoader. (set it to `false` to disable it)
202202
- `template`: to include custom HTML attributes for the TopLoader.
203203
- `zIndex`: defines zIndex for the TopLoader.
204-
- `showAtBottom`: To show the TopLoader at bottom. (increase height for the TopLoader to ensure it's visibility at the mobile devices)
205-
- `showForHashAnchor`: To show for "#" url or not. (set it to `false` to disable it)
204+
- `showAtBottom`: to show the TopLoader at bottom. (increase height for the TopLoader to ensure it's visibility at the mobile devices).
205+
- `showForHashAnchor`: to show for "#" url or not. (set it to `false` to disable it).
206+
- `nonce`: to add nonces to the `<style>` tags, for [Content Security Policy](https://www.w3.org/TR/2016/REC-CSP2-20161215/#script-src-the-nonce-attribute).
206207

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

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

225227
## Contributors
226228

src/index.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export type NextTopLoaderProps = {
8787
*
8888
*/
8989
showForHashAnchor?: boolean;
90+
/**
91+
* Nonce of the inline styles for [Content Security Policy](https://www.w3.org/TR/2016/REC-CSP2-20161215/#script-src-the-nonce-attribute).
92+
*/
93+
nonce?: string;
9094
};
9195

9296
/**
@@ -112,6 +116,7 @@ const NextTopLoader = ({
112116
zIndex = 1600,
113117
showAtBottom = false,
114118
showForHashAnchor = true,
119+
nonce,
115120
}: NextTopLoaderProps): React.JSX.Element => {
116121
const defaultColor = '#29d';
117122
const defaultHeight = 3;
@@ -135,7 +140,7 @@ const NextTopLoader = ({
135140
* CSS Styles for the NextTopLoader
136141
*/
137142
const styles = (
138-
<style>
143+
<style nonce={nonce}>
139144
{`#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)}}`}
140145
</style>
141146
);
@@ -245,7 +250,7 @@ const NextTopLoader = ({
245250

246251
// To debug the anchor target:
247252
// console.log('Given target is', (anchor as HTMLAnchorElement).target);
248-
const isExternalLink = ((anchor as HTMLAnchorElement).target as React.HTMLAttributeAnchorTarget) !== '' ;
253+
const isExternalLink = ((anchor as HTMLAnchorElement).target as React.HTMLAttributeAnchorTarget) !== '';
249254

250255
// Check for Special Schemes
251256
const isSpecialScheme = ['tel:', 'mailto:', 'sms:', 'blob:', 'download:'].some((scheme) =>

src/pages.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const PagesTopLoader = ({
2222
template,
2323
zIndex = 1600,
2424
showAtBottom = false,
25+
nonce,
2526
}: NextTopLoaderProps): JSX.Element => {
2627
const defaultColor = '#29d';
2728
const defaultHeight = 3;
@@ -34,8 +35,8 @@ export const PagesTopLoader = ({
3435
!shadow && shadow !== undefined
3536
? ''
3637
: shadow
37-
? `box-shadow:${shadow}`
38-
: `box-shadow:0 0 10px ${color},0 0 5px ${color}`;
38+
? `box-shadow:${shadow}`
39+
: `box-shadow:0 0 10px ${color},0 0 5px ${color}`;
3940

4041
// Check if to show at bottom
4142
const positionStyle = showAtBottom ? 'bottom: 0;' : 'top: 0;';
@@ -45,7 +46,7 @@ export const PagesTopLoader = ({
4546
* CSS Styles for the NextTopLoader
4647
*/
4748
const styles = (
48-
<style>
49+
<style nonce={nonce}>
4950
{`#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)}}`}
5051
</style>
5152
);

0 commit comments

Comments
 (0)