質問編集履歴

1

現在起きている問題について、より詳しく書き直しました。

2019/09/25 10:23

投稿

sezubo
sezubo

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,331 +1,329 @@
1
1
  Reactのルーティング周り、あるいはNginxの設定周りでハマってしまい、調べたのですがピンとくる情報がなかったので、質問させていただきます。
2
2
 
3
+ どうかよろしくお願いいたします。。。
4
+
5
+
6
+
7
+ ### 前提・実現したいこと
8
+
9
+
10
+
11
+ Reactで見た目を作り、`react-router-dom`でルーティングするアプリケーションを、webpackでビルドしてNginxで表示するようにしています。
12
+
13
+
14
+
15
+ 例えば`/user/edit`といったパスへのアクセスで、ユーザー編集画面を表示できるようにしたいのですが、現状できません。
16
+
17
+
18
+
19
+ `/user`や`/login`といったパスの深さが1次のものは期待通りのページが表示されますが、`/user/edit`のような深さが2次以上のページはうまく表示されません。
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+ コンポーネントのディレクトリ構成は以下のようになっています。
28
+
29
+
30
+
31
+ ```
32
+
33
+ ├── src
34
+
35
+ │   ├── App
36
+
37
+ │   │   ├── Footer
38
+
39
+ │   │   │   └── index.jsx
40
+
41
+ │   │   ├── Header
42
+
43
+ │   │   │   └── index.jsx
44
+
45
+ │   │   ├── Home
46
+
47
+ │   │   │   └── index.jsx
48
+
49
+ │   │   ├── Login
50
+
51
+ │   │   │   └── index.jsx
52
+
53
+ │   │   └── User
54
+
55
+ │   │   ├── Confirm
56
+
57
+ │   │   │   └── index.jsx
58
+
59
+ │   │   ├── Edit
60
+
61
+ │   │   │   └── index.jsx
62
+
63
+ │   │   └── index.jsx
64
+
65
+ ```
66
+
67
+
68
+
69
+ `react-router-dom"`でルーティングを制御している`src/index.jsx`は以下のようになっております。
70
+
71
+ パスとコンポーネントの対応付は、別で配列でもって、`.map()` でそれぞれルーティングを設定しています。
72
+
73
+
74
+
75
+ ```src/index.jsx
76
+
77
+
78
+
79
+ const routes = [
80
+
81
+ {
82
+
83
+ path: "/home",
84
+
85
+ component: Home,
86
+
87
+ name: "ホーム"
88
+
89
+ },
90
+
91
+ {
92
+
93
+ path: "/user/edit",
94
+
95
+ component: UserEdit,
96
+
97
+ name: "プロフィール編集"
98
+
99
+ },
100
+
101
+
102
+
103
+ (略)
104
+
105
+
106
+
107
+ {routes.map((route, index) => (
108
+
109
+ <Route
110
+
111
+ exaxt
112
+
113
+ key={index}
114
+
115
+ path={route.path}
116
+
117
+ pageName={route.name}
118
+
119
+ component={route.component}
120
+
121
+ />
122
+
123
+ ))}
124
+
125
+
126
+
127
+ ```
128
+
129
+
130
+
131
+ Webpackの設定は以下です。
132
+
133
+ 変な設定にはなっていないと思うのですが。。。
134
+
135
+
136
+
137
+ ```
138
+
139
+ const path = require("path");
140
+
141
+ const HtmlWebpackPlugin = require("html-webpack-plugin");
142
+
143
+ const Dotenv = require("dotenv-webpack");
144
+
145
+
146
+
147
+ const src = path.resolve(__dirname, "src");
148
+
149
+ const dist = path.resolve(__dirname, "dist");
150
+
151
+
152
+
153
+ module.exports = {
154
+
155
+ entry: ["@babel/polyfill", src + "/index.jsx"],
156
+
157
+ output: {
158
+
159
+ path: dist,
160
+
161
+ publicPath: "/",
162
+
163
+ filename: "[name]-[hash].bundle.js"
164
+
165
+ },
166
+
167
+ module: {
168
+
169
+ rules: [
170
+
171
+ {
172
+
173
+ enforce: "pre",
174
+
175
+ test: /.(js|jsx)$/,
176
+
177
+ exclude: /node_modules/,
178
+
179
+ use: [
180
+
181
+ {
182
+
183
+ loader: "eslint-loader",
184
+
185
+ options: {
186
+
187
+ formatter: require("eslint/lib/cli-engine/formatters/stylish")
188
+
189
+ }
190
+
191
+ }
192
+
193
+ ]
194
+
195
+ },
196
+
197
+ {
198
+
199
+ test: /.(js|jsx)$/,
200
+
201
+ exclude: /node_modules/,
202
+
203
+ loader: "babel-loader"
204
+
205
+ },
206
+
207
+ {
208
+
209
+ test: /.svg$/,
210
+
211
+ use: [
212
+
213
+ {
214
+
215
+ loader: "babel-loader"
216
+
217
+ },
218
+
219
+ {
220
+
221
+ loader: "react-svg-loader",
222
+
223
+ options: {
224
+
225
+ jsx: true
226
+
227
+ }
228
+
229
+ }
230
+
231
+ ]
232
+
233
+ }
234
+
235
+ ]
236
+
237
+ },
238
+
239
+ resolve: {
240
+
241
+ extensions: [".js", ".jsx"]
242
+
243
+ },
244
+
245
+ plugins: [
246
+
247
+ new HtmlWebpackPlugin({
248
+
249
+ template: src + "/public/index.html",
250
+
251
+ publicPath: "/",
252
+
253
+ filename: "index.html"
254
+
255
+ }),
256
+
257
+ new Dotenv({
258
+
259
+ path: "./.env"
260
+
261
+ })
262
+
263
+ ]
264
+
265
+ };
266
+
267
+
268
+
269
+ ```
270
+
271
+
272
+
273
+
274
+
275
+ Nginxの設定は以下です。
276
+
277
+ ここも変なことはしていないと思っているのですが、なんとなく`try_files $uri $uri/ /index.html;`のあたりがまずいのかなと思っていたりします。。。
278
+
279
+
280
+
281
+ ```
282
+
283
+ server {
284
+
285
+ listen 80;
286
+
287
+ server_name localhost;
288
+
289
+ location / {
290
+
291
+ root /var/www/html;
292
+
293
+ index index.html;
294
+
295
+ try_files $uri $uri/ /index.html;
296
+
297
+ }
298
+
299
+ error_page 500 502 503 504 /50x.html;
300
+
301
+ location = /50x.html {
302
+
303
+ root /usr/share/nginx/html;
304
+
305
+ }
306
+
307
+ }
308
+
309
+
310
+
311
+ ```
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+ ### 発生している問題・エラーメッセージ
320
+
321
+
322
+
323
+ エラーメッセージは発生せず、`/user/edit`にアクセスすると、Userコンポーネントが表示されてしまいます。(UserEditコンポーネントが表示されてほしい)
324
+
325
+
326
+
327
+ どのようにすれば、`/user/edit` でUserEditコンポーネントを表示することができるでしょうか?
328
+
3
329
  何卒よろしくお願いいたします。
4
-
5
-
6
-
7
- ### 前提・実現したいこと
8
-
9
-
10
-
11
- Reactで見た目を作り、`react-router-dom`でルーティングするアプリケーションを、webpackでビルドしてNginxで表示するようにしています。
12
-
13
-
14
-
15
- 例えば`/user/edit`といったパスへのアクセスで、ユーザー編集画面を表示できるようにしたいのですが、現状できません。
16
-
17
-
18
-
19
- `/user`や`/login`といったパスの深さが1次のものは期待通りのページが表示されますが、`/user/edit`のような深さが2次以上のページはうまく表示されません。
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
- コンポーネントのディレクトリ構成は以下のようになっています。
28
-
29
-
30
-
31
- ```
32
-
33
- ├── src
34
-
35
- │   ├── App
36
-
37
- │   │   ├── Footer
38
-
39
- │   │   │   └── index.jsx
40
-
41
- │   │   ├── Header
42
-
43
- │   │   │   └── index.jsx
44
-
45
- │   │   ├── Home
46
-
47
- │   │   │   └── index.jsx
48
-
49
- │   │   ├── Login
50
-
51
- │   │   │   └── index.jsx
52
-
53
- │   │   └── User
54
-
55
- │   │   ├── Confirm
56
-
57
- │   │   │   └── index.jsx
58
-
59
- │   │   ├── Edit
60
-
61
- │   │   │   └── index.jsx
62
-
63
- │   │   └── index.jsx
64
-
65
- ```
66
-
67
-
68
-
69
- `react-router-dom"`でルーティングを制御している`src/index.jsx`は以下のようになっております。
70
-
71
-
72
-
73
- ```src/index.jsx
74
-
75
-
76
-
77
- const routes = [
78
-
79
- {
80
-
81
- path: "/home",
82
-
83
- component: Home,
84
-
85
- name: "ホーム"
86
-
87
- },
88
-
89
- (略)
90
-
91
- {
92
-
93
- path: "/user/edit",
94
-
95
- component: UserEdit,
96
-
97
- name: "プロフィール編集"
98
-
99
- },
100
-
101
- {
102
-
103
- path: "/user_confirm",
104
-
105
- component: UserEditConfirm,
106
-
107
- name: "プロフィール編集"
108
-
109
- }
110
-
111
- ];
112
-
113
-
114
-
115
- (略)
116
-
117
-
118
-
119
- {routes.map((route, index) => (
120
-
121
- <Route
122
-
123
- exaxt
124
-
125
- key={index}
126
-
127
- path={route.path}
128
-
129
- pageName={route.name}
130
-
131
- component={route.component}
132
-
133
- />
134
-
135
- ))}
136
-
137
-
138
-
139
- ```
140
-
141
-
142
-
143
- Webpackの設定は以下です。
144
-
145
-
146
-
147
- ```
148
-
149
- const path = require("path");
150
-
151
- const HtmlWebpackPlugin = require("html-webpack-plugin");
152
-
153
- const Dotenv = require("dotenv-webpack");
154
-
155
-
156
-
157
- const src = path.resolve(__dirname, "src");
158
-
159
- const dist = path.resolve(__dirname, "dist");
160
-
161
-
162
-
163
- module.exports = {
164
-
165
- entry: ["@babel/polyfill", src + "/index.jsx"],
166
-
167
- output: {
168
-
169
- path: dist,
170
-
171
- publicPath: "/",
172
-
173
- filename: "[name]-[hash].bundle.js"
174
-
175
- },
176
-
177
- module: {
178
-
179
- rules: [
180
-
181
- {
182
-
183
- enforce: "pre",
184
-
185
- test: /.(js|jsx)$/,
186
-
187
- exclude: /node_modules/,
188
-
189
- use: [
190
-
191
- {
192
-
193
- loader: "eslint-loader",
194
-
195
- options: {
196
-
197
- formatter: require("eslint/lib/cli-engine/formatters/stylish")
198
-
199
- }
200
-
201
- }
202
-
203
- ]
204
-
205
- },
206
-
207
- {
208
-
209
- test: /.(js|jsx)$/,
210
-
211
- exclude: /node_modules/,
212
-
213
- loader: "babel-loader"
214
-
215
- },
216
-
217
- {
218
-
219
- test: /.svg$/,
220
-
221
- use: [
222
-
223
- {
224
-
225
- loader: "babel-loader"
226
-
227
- },
228
-
229
- {
230
-
231
- loader: "react-svg-loader",
232
-
233
- options: {
234
-
235
- jsx: true
236
-
237
- }
238
-
239
- }
240
-
241
- ]
242
-
243
- }
244
-
245
- ]
246
-
247
- },
248
-
249
- resolve: {
250
-
251
- extensions: [".js", ".jsx"]
252
-
253
- },
254
-
255
- plugins: [
256
-
257
- new HtmlWebpackPlugin({
258
-
259
- template: src + "/public/index.html",
260
-
261
- publicPath: "/",
262
-
263
- filename: "index.html"
264
-
265
- }),
266
-
267
- new Dotenv({
268
-
269
- path: "./.env"
270
-
271
- })
272
-
273
- ]
274
-
275
- };
276
-
277
-
278
-
279
- ```
280
-
281
-
282
-
283
-
284
-
285
- Nginxの設定は以下です。
286
-
287
-
288
-
289
- ```
290
-
291
- server {
292
-
293
- listen 80;
294
-
295
- server_name localhost;
296
-
297
- location / {
298
-
299
- root /var/www/html;
300
-
301
- index index.html;
302
-
303
- try_files $uri $uri/ /index.html;
304
-
305
- }
306
-
307
- error_page 500 502 503 504 /50x.html;
308
-
309
- location = /50x.html {
310
-
311
- root /usr/share/nginx/html;
312
-
313
- }
314
-
315
- }
316
-
317
-
318
-
319
- ```
320
-
321
-
322
-
323
- なんとなく`try_files $uri $uri/ /index.html;`のあたりがまずいのかなと思っていますが、どう直せばよいのかがわかりません。。。
324
-
325
-
326
-
327
- ### 発生している問題・エラーメッセージ
328
-
329
-
330
-
331
- エラーメッセージは発生せず、`/user/edit`にアクセスすると、Userコンポーネントが表示されてしまいます。(UserEditコンポーネントが表示されてほしい)