---
title: "JSSを用いたカスタムレイアウトサービス構成の使用"
slug: "using-a-custom-layout-service-configuration-with-jss"
abstract: "JSSアプリでカスタムレイアウトサービス構成の使い方"
---
# JSSを用いたカスタムレイアウトサービス構成の使用

レイアウトサービスのJSONレンダリングプロセスは非常に設定可能で、レイアウトサービスの特定の側面をカスタマイズできます。

jssレイアウトサービス構成の任意の要素をカスタマイズしたい場合は、ref属性を使って既存の構成をコピーし、その要素内でカスタマイズすることをお勧めします。これにより、アップグレード時にjss設定が変更された場合に設定の変更を減らすことができます。

> !重要JSSで使用されるレイアウトサービスの設定を変更すると、オブジェクトの形状、シリアル化、プレースホルダー処理に変更が生じると、JSS SDKが破損し、JSSインポートプロセスの前提と矛盾することがあります。

シリアライゼーションの設定を変更することで、ユーザーが影響できないJSONデータを変更することができます。シリアライゼーションの設定を変更する際は、JSON出力全体を変更するか、フィールド名だけを変更することができます。例えば、フィールド名をキャメルケースにしたい場合に変更できます。

カスタム構成を定義した後、renderJsonRenderingパイプラインの<AllowedConfigurations>リストに設定名を追加します。これを行うことで、デフォルトのjss設定で実行されるPlaceholderTransformerコードがカスタム設定でも実行されます。

> !注このステップは、カスタム構成がデフォルトのJSS構成と形状が似ている場合にのみ関係します。カスタム構成がJSSと関連しない場合や、デフォルトのjss構成に依存していない場合は、このステップは不要です。

以下は、デフォルトのjss構成に基づくカスタムレイアウトサービス構成の「my-jss-config」のSitecore構成パッチです。

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
 <layoutService>
 <configurations>
 <!-- Define your custom named config, using the \`ref\` attribute to "copy" the existing \`jss\` configuration -->
 <config name="my-jss-config">
 <rendering ref="/sitecore/layoutService/configurations/config@name='jss'/rendering">
 <!-- Override placeholdersResolver -->
 <placeholdersResolver type="My.Resolver, My.Assembly">
 </rendering>
 <serialization ref="/sitecore/layoutService/configurations/config@name='jss'/serialization" />
 </config>
 </configurations>
 </layoutService>
 <pipelines>
 <group groupName="layoutService">
 <pipelines>
 <renderJsonRendering>
 <processor type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.Pipelines.RenderJsonRendering.AddComponentName, Sitecore.JavaScriptServices.ViewEngine" resolve="true">
 <AllowedConfigurations hint="list">
 <!-- Custom named config is added to this list -->
 <config id="2">my-jss-config</config>
 </AllowedConfigurations>
 </processor>
 </renderJsonRendering>
 </pipelines>
 </group>
 </pipelines>
 </sitecore>
</configuration>

カスタム設定をパッチで取り付けた後、JSSアプリでlayoutServiceConfiguration属性を使って活用できます。

<javaScriptServices>
 <apps>
 <app name="MyApp"
 sitecorePath="/sitecore/content/MyApp"
 layoutServiceConfiguration="my-jss-config"
 inherits="defaults"
 />
 </apps>
</javaScriptServices>

この設定名はクライアントコード内およびdataApiを使ってレイアウトサービスを呼び出す際にも必ず記載してください(前述の例を参照)。

## レイアウトサービス構成

レイアウトサービスの構成は、/sitecore/layoutService/configurationsパスのSitecore構成で見つけることができます。

<config name="jss">
 <!--
 An implementation of \`Sitecore.LayoutService.Configuration.IRenderingConfiguration\`.
 Allows the addition of filtering logic to output renderings and placeholders.
 -->
 <rendering type="Sitecore.LayoutService.Configuration.DefaultRenderingConfiguration, Sitecore.LayoutService">

 <!--
 An implementation of \`Sitecore.LayoutService.Placeholders.IPlaceholdersResolver\`.
 Extracts the exposed placeholders of a rendering. Also available is
 \`Sitecore.LayoutService.Placeholders.SimplePlaceholdersResolver\`.
 -->
 <placeholdersResolver type="Sitecore.LayoutService.Placeholders.DynamicPlaceholdersResolver, Sitecore.LayoutService"/>

 <!--
 An implementation of \`Sitecore.LayoutService.Serialization.ItemSerializers.IItemSerializer\`.
 Determines what fields of an item should be serialized and writes them out as JSON.
 The default implementation filters standard fields. Also available is
 \`Sitecore.LayoutService.Serialization.ItemSerializers.AllFieldsItemSerializer\`.
 -->
 <itemSerializer type="Sitecore.LayoutService.Serialization.ItemSerializers.DefaultItemSerializer, Sitecore.LayoutService" resolve="true"/>

 <!--
 An implementation of \`Sitecore.LayoutService.ItemRendering.ContentsResolvers.IRenderingContentsResolver\`.
 This is the default contents resolver -- it can be overridden on a per-rendering basis, as described above.
 -->
 <renderingContentsResolver type="Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver, Sitecore.LayoutService">
 <IncludeServerUrlInMediaUrls>true</IncludeServerUrlInMediaUrls>
 </renderingContentsResolver>
 </rendering>

 <!--
 An implementation of Sitecore.LayoutService.Configuration.ISerializationConfiguration.
 Controls the \`JsonSerializerSettings\` used by the Layout Service and any transformation of the resulting JSON.
 The JSS \`SerializationConfiguration\` customizes JSON serialization to preserve the case of dictionary keys.
 -->
 <serialization type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.SerializationConfiguration, Sitecore.JavaScriptServices.ViewEngine">

 <!--
 An implementation of \`Sitecore.LayoutService.Serialization.ILayoutTransformer\`.
 Provides full control over the shape of the output JSON to optimize it for the use of specific client needs.
 JSS optimizes the output for JavaScript consumption.
 -->
 <transformer type="Sitecore.JavaScriptServices.ViewEngine.LayoutService.Serialization.LayoutTransformer, Sitecore.JavaScriptServices.ViewEngine"/>
 </serialization>
</config>

configノードのname属性はレイアウトサービスのURLのconfigパラメータに対応します。

/sitecore/api/layout/render/config?item=path&sc\_lang=language&sc\_apikey=key&tracking=true|false