# 推迟javascript并在初始页面加载时显示中间内容

**URL:** https://meta.discourse.org/t/defer-javascript-and-show-interim-content-on-initial-page-load/216458
**Category:** Development
**Created:** [2022年一月30日 02:02 UTC](https://meta.discourse.org/t/defer-javascript-and-show-interim-content-on-initial-page-load/216458 "2022-01-30T02:02:16Z")
**Posts on this page:** 1
**Showing post:** 14

<div class="post-metadata">

### Author: ![rrit](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@rrit](https://meta.discourse.org/u/rrit)
#### Post date: [2022年二月7日 20:33 UTC](https://meta.discourse.org/t/defer-javascript-and-show-interim-content-on-initial-page-load/216458/14 "2022-02-07T20:33:55Z")

</div>

> [@david](#):
>
> @rrit，你能在你的 PR 中添加一个站点设置吗？

功能标志已实现。

> [@david](#):
>
> RSpec 测试以验证启用/禁用该设置的行为。

我完全不是 Ruby 开发者——在这方面我肯定需要一些帮助。

~~也许我应该将我的 POC 推送到 discourse/discourse 仓库的一个新分支，然后再向 `main` 发送 PR？~~

这是我关于此功能的 PR：

> <https://github.com/discourse/discourse/pull/15858>
>
> Implement experimental feature flags for "static topic content" and " javascript… defer"
> 
> \### static topic content
> 
> \`SiteSetting.enable\_experimental\_static\_topic\_content\`
> 
> Can be set via settings dashboard.
> 
> Show static content in topic view for a faster Largest Contentful Paint (LCP).
> Warning: visitors might see disconcerting jumping of content!"
> 
> \### javascript defer
> 
> \`SiteSetting.enable\_experimental\_javascript\_defer\`
> 
> Can be set via settings dashboard.
> 
> Defer loading of JavaScripts to show static content even faster in combination with 'enable\_experimental\_javascript\_defer'. Warning: the discourse frontend, themes, components and/or plugins might fail!
> 
> \---
> 
> The vendor-javascript and all preceding javascripts are not deferred right now.
> @see: https://github.com/rr-it/discourse/commit/49405c353a31180933aed9dca7697dce05227707
> Ideas on how to solve this are very welcome.
> 
> \---
> 
> For more information see also:
> https://meta.discourse.org/t/defer-javascript-and-show-interim-content-on-initial-page-load/216458

@david 你能帮我开发这些更改的 Rspec 测试吗：

[app/helpers/application\_helper.rb](https://github.com/discourse/discourse/pull/15858/files#diff-5f3fc5e3977d242572aa1d08551f5eb557de0ccaff30370838ee9df5386ea0da)：[spec/helpers/application\_helper\_spec.rb](https://github.com/discourse/discourse/pull/15858/commits/da1ad9bcda6fe013eb4edde0b88cf86923c76436?diff=split&w=1#diff-caa3def9981603cfcc31966f2bbe59aba2777727934cb2f06a375510582584c1)

我在这里看不到可行的单元测试。它似乎只能通过集成测试进行测试。  
 ~~[app/models/theme.rb](https://github.com/discourse/discourse/pull/15858/files#diff-415da1b21008116467704855fa96412f74bec76fed4013f1f53653523adffa54)~~  
 ~~[app/models/theme\_field.rb](https://github.com/discourse/discourse/pull/15858/files#diff-4d55893c87d5416eb58dfddfdc147764c722c431356f3003956f317d20497560)~~

我不得不禁用 QUnit 测试运行器的 defer 标签：[app/views/qunit/index.html.erb](https://github.com/discourse/discourse/pull/15858/commits/727426aadaea3c6de4f09306db81d0ca38b38f90#diff-fbee51a96326b48b47f2bcfeffb2ae5dcc21c65cd4761f47fec5a3263be17bf2)  
之前 QUnit 测试在功能标志 `"javascript defer" = false` 时仍然运行。现在，即使在 `"javascript defer" = true` 的情况下，测试也能运行。

---

_[View the full topic](https://meta.discourse.org/t/defer-javascript-and-show-interim-content-on-initial-page-load/216458)._
