您如何在不同的构建环境中使用 Contentful Middleman 预览 API?

How do you use the Contentful Middleman preview API in different build environment?

这是我在原则上试图实现的目标,这显然行不通,因为需要在构建之前检索内容数据:

# Build command
bundle exec middleman contentful && bundle exec middleman build -e <ENV>
# config.rb

configure :prod do
  set :build_dir, 'build/prod'
  set :ENV, 'prod'

  activate :contentful do |f|
    f.space         = { hs: 'SPACE' }
    f.access_token  = 'TOKEN'
    f.cda_query     = { include: 2 }
    f.all_entries   = true
    f.content_types = { #... }
  end
end

configure :testSite do
  set :build_dir, 'build/test-site'
  set :ENV, 'test'

  activate :contentful do |f|
    use_preview_api = true
    #...
  end
end

您可以使用环境变量来开启它。

# Build command
CONTENTFUL_ENV=<env> bundle exec middleman contentful && bundle exec middleman build -e <env>


# config.rb
access_token = nil
use_preview = false
case ENV['CONTENTFUL_ENV']
when 'prod'
  access_token = ENV['CONTENTFUL_PROD_TOKEN']
when 'testSite'
  access_token = ENV['CONTENTFUL_TEST_TOKEN']
  use_preview = true
end

activate :contentful do |f|
  f.space = {space_alias: ENV['CONTENTFUL_SPACE']}
  f.use_preview_api = use_preview
  f.access_token = access_token
  # ...
end

这样您就可以让多个环境协同工作