Skip to content
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

Dependency Footer Scripts #1105

Closed
khrome83 opened this issue Feb 13, 2017 · 6 comments
Closed

Dependency Footer Scripts #1105

khrome83 opened this issue Feb 13, 2017 · 6 comments

Comments

@khrome83
Copy link
Contributor

Question:

Originally I was considering to use <Head> to load a polyfill if a specific component configured in a specific way.

Is there a way to say to inject a script to be included with the <NextScript /> block from within another component. The same way that we include a script within the head.

An example -

I built a image component that can also be a responsive image using srcset and sizes. I want to include the picturefill.min.js library if either of these attributes are not undefined.

@arunoda
Copy link
Contributor

arunoda commented Feb 14, 2017

If you are looking for this, that's possible:

export default class extends Document {
  render() {
    return (
      <html>
        <Head />
        <body>
          <Main />
          <NextScript />
          <script src="your location" />
        </body>
      </html>
    );
  }
}

@khrome83
Copy link
Contributor Author

khrome83 commented Feb 14, 2017 via email

@arunoda
Copy link
Contributor

arunoda commented Feb 14, 2017

@khrome83 Normally react don't allow you to use script tags.
But you could use a NPM module like this: https://www.npmjs.com/package/load-script

Then it loads after NextScripts for sure.

@khrome83
Copy link
Contributor Author

Understood. I guess in my mind I was thinking of something along the lines of how we use <HEAD>

import ScriptRender from 'next/something';

function Comonent() {
  return (
    ....

    some unique functionality goes here that requires special outside library
    examples - D3.js, Chart.js, PictureFill
   
    ....
    <ScriptRender key="d3">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/picturefill/3.0.2/picturefill.js" integrity="sha256-uttYJd+gaT1hgbYhYhGbIeFa1yK6JRnE6TVRZXroqIM=" crossorigin="anonymous"></script>
    </ScriptRender>
  );
}

export default Component;

Then in the _document.js

import Document, { Main, NextScript, RenderScripts } from 'next/document';

export default class MyDocument extends Document {
  static async getInitialProps({ renderPage }) {
    const page = renderPage();
    return { ...page };
  }

  render() {
    return (
      <html lang="en">
        <head>
          <meta charSet="utf-8" />
          <meta name="robots" content="index, follow" />
          <meta name="author" content="SomeSite" />
          <meta name="viewport" content="initial-scale=1.0, width=device-width" />
        </head>
        <body>
          <Main />
          <NextScript />
          <RenderScripts />
        </body>
      </html>
    );
  }
}

Thoughts?

The idea is that if I have 20 pages, and I am using something on two of those pages. I should only have the _document.js render that outside resource if it is needed on that page. The key would identity it making sure it does not re-render and replace it.

@arunoda
Copy link
Contributor

arunoda commented Feb 20, 2017

@khrome83 something like ScriptRender can be build without any help from Next.js. Since this is not something every app needs, we won't be having something like this in the core.

Check these solutions: https://goo.gl/Z1Dxsq

@arunoda arunoda closed this as completed Feb 20, 2017
@khrome83
Copy link
Contributor Author

khrome83 commented Jun 3, 2017

@arunoda - sorry i just saw this very late. If you have time, would you be able to link the solution you were trying to share. The tiny google url apparently timed out.

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants