0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

NodeInvocationException:document is not definedのエラーが出る

Last updated at Posted at 2018-11-09

NodeInvocationException:document is not defined

.NET CoreとAngularの開発において、複数のパターンでこのエラーが出た。
ソースのサーバサイドレンダリング設定箇所を修正し、解決した。

開発環境

Windows 10 ver 10.0.17134
Visual Studio Community 2017 ver 15.7.1
.NET Framework ver 4.7.03056
.NET Core Framework ver 2.0.7
Angular ver 4.2.5
ブラウザ Fire fox ver 63.0.1

問題が発生した状況

ASP.NET CoreとAngularを使用してWebサイトを開発していると、二つのパターンで同じエラーが出るようになった。
一つ目は、以下の画面上でボタンをクリックしたとき。
キャプチャ40.PNG

このようなエラーになる。
キャプチャ41.PNG

もう一つは、URLにパラメタが含まれる以下の画面を開き、ブラウザ再表示を行ったとき。
キャプチャ31.PNG

同じエラーになる。
キャプチャ30.PNG

対処方法

初回の表示は問題がなく、それぞれリダイレクトした後でエラーが出ている。そのため、サーバーサイドレンダリングの関係かと思って調べていたところ、以下のサイトで解決方法を発見した。

ASP.NET CORE 2 + ANGULAR 4: NodeInvocationException: Prerendering failed because of error: ReferenceError: window is not defined

内容は、Views\Home\Index.cshtmlの、サーバサイドレンダリングを有効にするモジュール設定項目を書き換える。

Index.cshtml
@{
    ViewData["Title"] = "Home Page";
}

@*<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app> この行を削除する。*@
<app asp-ng2-prerender-module="ClientApp/dist/main-server">Loading...</app> @*この行を追加する。*@

<script src="~/dist/vendor.js" asp-append-version="true"></script>
@section scripts {
    <script src="~/dist/main-client.js" asp-append-version="true"></script>
}

Angularを使っている場合は上記が解決方法になるが、他にもサーバサイドレンダリングをしない方法もある。

Index.cshtml
<app>Loading...</app>

余談:サーバサイドレンダリングの速度調査結果

サーバサイドレンダリングを使用する場合としない場合とのレスポンスの速度を、開発中のサイトで計測してみた(上記asp-ng2-prerender-moduleを指定する場合と、記載しない場合)。本番環境で想定しているデータ量の範囲内だと、どちらも500ms未満で返ってきたため、違いが分からなかった。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?