简体   繁体   English

Nuxt.js static 网站相关网址

[英]Nuxt.js static site relative urls

I've created a new static Nuxt.js project.我创建了一个新的 static Nuxt.js 项目。 All the urls and paths to resources are absolute ie / .所有资源的 url 和路径都是绝对的,即/ Is there an easy way to use relative urls and paths ./ ?有没有一种简单的方法来使用相对 url 和路径./

I have a few assumptions (I'm a Java dev):我有一些假设(我是 Java 开发人员):

  • Static Nuxt.js projects do not require a web server. Static Nuxt.js 项目不需要 web 服务器。
  • /file.js is intended to be served by a web server. /file.js旨在由 web 服务器提供服务。

The projects purpose is reading data (json files) locally and displaying the processed data (hence no web server).该项目的目的是在本地读取数据(json 文件)并显示处理后的数据(因此没有 web 服务器)。 I'm going to add fancy graphs, forms etc... Am I using the wrong approach?我要添加精美的图表,forms 等...我使用了错误的方法吗?

I've spent a long time googling this and I've never seen a decent answer.我花了很长时间在谷歌上搜索这个,我从来没有看到一个像样的答案。

Here is my nuxt.conf.js config.output.publicPath = './_nuxt/' seems to do half a job.这是我的 nuxt.conf.js config.output.publicPath = './_nuxt/'似乎完成了一半的工作。 It works works for <script src="./_nuxt/936eba5.js"></script> but not <link rel="preload" href="/_nuxt/936eba5.js" as="script">它适用于<script src="./_nuxt/936eba5.js"></script>但不适用于<link rel="preload" href="/_nuxt/936eba5.js" as="script">

export default {
  // Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
  ssr: false,

  // Target: https://go.nuxtjs.dev/config-target
  target: 'static',

  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'my-app',
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
  ],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/typescript
    '@nuxt/typescript-build',
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/buefy
    'nuxt-buefy',
  ],

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    extend (config, { isDev, isClient }) {
      if (!isDev) {
        // relative links, please.
        config.output.publicPath = './_nuxt/'
      }
      return config;
    }
  },

  // router: {
  //   base: ''
  // }
}

EDIT with more information.编辑更多信息。

_nuxt is the default path/url that is in /dist . _nuxt/dist中的默认路径/url。

I've only generated a project at this point and tried to make the urls relative.我此时只生成了一个项目并尝试使网址相对。

/dist

  • 200.html 200.html
  • README.md自述文件.md
  • _nuxt _nuxt
  • favicon.ico favicon.ico
  • index.html索引.html
  • inspire启发

So removing config.output.publicPath = './_nuxt/' .所以删除config.output.publicPath = './_nuxt/' Using ssr: true .使用ssr: true Then running npm run generate然后运行npm run generate

These absolute url/paths are generated:生成这些绝对 url/路径:

<link rel="preload" href="/_nuxt/42fc731.js" as="script">
<script src="/_nuxt/42fc731.js" defer></script>

It's been raised as a bug many times in Nuxt.js Github but I've never seen an actual resolution.在 Nuxt.js Github 中多次将其作为错误提出,但我从未见过实际的分辨率。

If you want to load some JSONs into your Nuxt app, you should use nuxt-content .如果你想将一些 JSON 加载到你的 Nuxt 应用程序中,你应该使用nuxt-content I've actually created a boilerplate here that you can check: https://github.com/kissu/nuxt-tailwind-typography-darkmode-boilerplate我实际上在这里创建了一个样板文件,您可以查看: https://github.com/kissu/nuxt-tailwind-typography-darkmode-boilerplate

You can find 2 articles in /content/articles .您可以在/content/articles中找到 2 篇文章。 There, you can see how to fetch then and how to display them.在那里,您可以看到如何获取它们以及如何显示它们。

Full static Nuxt do not require any server indeed.完整的 static Nuxt 确实不需要任何服务器。 You can even SSR ( ssr: true ) it and still have it as target: static .您甚至可以使用 SSR ( ssr: true ) 并将其作为target: static

publicPath is not the right thing here. publicPath在这里不是正确的。 It should not be used, neither should you use any of the following: _nuxt , .nuxt or alike.不应使用它,也不应使用以下任何一种: _nuxt.nuxt等。
yarn generate , then deploy your /dist directory to a simple free platform, like Netlify and you'll be gucci. yarn generate ,然后将您的/dist目录部署到一个简单的免费平台,例如Netlify ,您将成为 gucci。 You can try my project if you want (beware, there is more configuration than neeed in your case).如果需要,您可以尝试我的项目(请注意,您的情况需要的配置多于您的需要)。

If you want to add something to your head , please follow this documentation: https://nuxtjs.org/docs/2.x/features/meta-tags-seo如果您想在head中添加一些东西,请遵循以下文档: https://nuxtjs.org/docs/2.x/features/meta-tags-seo

Nuxt.js doesn't appear to support relative urls (see above). Nuxt.js 似乎不支持相对网址(见上文)。 However Vue3 and Vite does.但是 Vue3 和 Vite 可以。

Reproduction steps:复制步骤:

npm install vue@next
npm install -g @vue/cli
npm init @vitejs/app my-app

output: output:

@vitejs/create-app
Ok to proceed? (y) y
✔ Select a framework: · vue
✔ Select a variant: · TypeScript
cd my-app
npm install
npm run build

vite.config.ts vite.config.ts

import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  base: './'
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM