简体   繁体   English

如何使用服务器端渲染在 vue/nuxt 中从客户端隐藏代码?

[英]How to hide code from client side in vue/nuxt, using Server Side Rendering?

I am trying to do some processing on the server side, which I do not want to be viewable on the client side.我正在尝试在服务器端进行一些处理,但我不想在客户端看到这些处理。

I have successfully tried using either fetch or asyncData to populate the state, but I do not want the process followed to be available on the browser.我已成功尝试使用fetchasyncData来填充 state,但我不希望浏览器上可以使用后续过程。

For example:例如:

<template>
  // ...
</template>

<script>
import ...

export default {
  layout: 'layout1',

  name: 'Name',

  components: { ... },

  data: () => ({ ... }),

  computed: { ... },

  async asyncData({ store }) {

    const news = await axios.get(
      'https://newsurl.xml'
    ).then(feed =>
         // parse the feed and do some very secret stuff with it
         // including sha256 with a salt encryption
    )
    store.commit('news/ASSIGN_NEWS', news)
  }
}
</script>

I want the code in asyncData (or in fetch ) not to be visible on the client side.我希望asyncData (或fetch )中的代码在客户端不可见。

Any suggestion will be appreciated.任何建议将不胜感激。

You can use onServerPrefetch hook.您可以使用onServerPrefetch钩子。

onServerPrefetch(async () => {
  const news = await axios.get(
  'https://newsurl.xml'
).then(feed =>
     // parse the feed and do some very secret stuff with it
     // including sha256 with a salt encryption
)
  this.$store.commit('news/ASSIGN_NEWS', news)
})

There are several questions like this one already, one of which I've answered here and here .已经有几个类似的问题,我已经在此处和此处回答了其中一个问题。

The TLDR being that if you want to make something on server only, you probably don't even want it part of a .vue file from the beginning. TLDR 是,如果你只想在服务器上制作一些东西,你可能从一开始就不希望它成为.vue文件的一部分。 Using a backend proxy could also be useful (as stated above), especially if you could benefit from caching or reduce bandwidth overall.使用后端代理也很有用(如上所述),尤其是当您可以从缓存中受益或减少整体带宽时。

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

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