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

Using NUXT.js just for static html generation with script (vendor.js, manifest.js, app,js, pages/index.js, layouts/default.js) #2822

Closed
igorify opened this issue Feb 12, 2018 · 7 comments
Labels

Comments

@igorify
Copy link

igorify commented Feb 12, 2018

Hi, guys! Can you anyone help me?

Does nuxt have any option to exclude _nuxt scripts in generate mode?

I need to generate only static html version of Vue app.
Without _nuxt folder and included links and script.
I dont need to use javascript on client and those scripts in _nuxt` former makes my project to heavy.

Basically, I want to use NUXT just for static html generation, NOT Pre-rendering with spa on client.

<link href="/_nuxt/manifest.10d0ff23a2bf4f09b86d.js" rel="preload" as="script"><link href="/_nuxt/vendor.2c4933a76531d246be17.js" rel="preload" as="script"><link href="/_nuxt/app.b868b05b142119955871.js" rel="preload" as="script"><link href="/_nuxt/layouts/default.860485f7694926d31a51.js" rel="preload" as="script"><link href="/_nuxt/pages/index.f4640f8dc2b172dfb993.js" rel="preload" as="script"><link>

<script type="text/javascript">window.__NUXT__={layout:"default",data:[{}],error:null,serverRendered:!0}</script><script defer="" src="/_nuxt/manifest.10d0ff23a2bf4f09b86d.js"></script><script defer="" src="/_nuxt/layouts/default.860485f7694926d31a51.js"></script><script defer="" src="/_nuxt/pages/index.f4640f8dc2b172dfb993.js"></script><script defer="" src="/_nuxt/vendor.2c4933a76531d246be17.js"></script><script defer="" src="/_nuxt/app.b868b05b142119955871.js"></script>

Thanks!

This question is available on Nuxt.js community (#c2450)
@igorify igorify changed the title Can Nuxt generate static html without _nuxt scripts? Using NUXT.js just for static html generation Feb 12, 2018
@igorify
Copy link
Author

igorify commented Feb 13, 2018

Update!
I have found to set render: { resourceHints: false } at nuxt.config.js

This allow to not include this links.

<link href="/_nuxt/manifest.10d0ff23a2bf4f09b86d.js" rel="preload" as="script"><link href="/_nuxt/vendor.2c4933a76531d246be17.js" rel="preload" as="script"><link href="/_nuxt/app.b868b05b142119955871.js" rel="preload" as="script"><link href="/_nuxt/layouts/default.860485f7694926d31a51.js" rel="preload" as="script"><link href="/_nuxt/pages/index.f4640f8dc2b172dfb993.js" rel="preload" as="script"><link>

But the problem this including nuxt system script are still open

@igorify igorify changed the title Using NUXT.js just for static html generation Using NUXT.js just for static html generation with script (vendor.js, manifest.js, app,js, pages/index.js, layouts/default.js) Feb 13, 2018
@ederchrono
Copy link

I have something like this in the nuxt.config file, inside the module.exports object:

hooks: {
    // This hook is called before rendering the html to the browser
    'render:route': (url, result) => {
      this.$ = cheerio.load(html)
      this.$('#__nuxt').removeAttr('data-server-rendered').removeAttr('id')
      this.$(`body script[src="/_nuxt/app.js"]`).remove()
      this.$(`head link[href="/_nuxt/app.js"]`).remove()
      result.html = this.$.html()
    }
}

You just need to install cheerio https://github.com/cheeriojs/cheerio, and add any other rule to remove other scripts

@garredow
Copy link

Building off of igorify and ederchrono's solutions, I ended up with this to strip the scripts when generating static pages. In my nuxt.config.js, I have:

render: { resourceHints: false },
hooks: {
  'generate:page': page => {
    const doc = cheerio.load(page.html);
    doc(`body script`).remove();
    page.html = doc.html();
  },
},

@SpikeShape
Copy link

@garredow thanks for your workaround. Since I only want to use NUXT as an HTML rendering engine it would be nice to have some options to get rid of all SPA features.

@SpikeShape
Copy link

SpikeShape commented Jul 3, 2018

I ended up using the prerender-spa-plugin which opens the page in a headless chromium and saves the resulting HTML to .html files. It also allows for multiple routes and so on This might be what you are looking for. I could provide example config in case anyone is interested.

Nuxt is built around the idea of delivering the initial page as HTML while rendering all the following changes on the client side. Since you don't want that you might want to let go of nuxt until they allow for a pre-render option or something like that.

@ghost
Copy link

ghost commented Aug 18, 2018

This question has been resolved by @ederchrono, see answer.

@ghost ghost added the cmty:status:resolved label Aug 18, 2018
@lock
Copy link

lock bot commented Nov 1, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 1, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants