@remix-run/router 1.0.3-pre.0 → 1.0.3-pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/router.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @remix-run/router v1.0.3-pre.0
2
+ * @remix-run/router v1.0.3-pre.1
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -116,6 +116,10 @@ function createMemoryHistory(options) {
116
116
  return typeof to === "string" ? to : createPath(to);
117
117
  },
118
118
 
119
+ encodeLocation(location) {
120
+ return location;
121
+ },
122
+
119
123
  push(to, state) {
120
124
  action = Action.Push;
121
125
  let nextLocation = createMemoryLocation(to, state);
@@ -347,6 +351,14 @@ function parsePath(path) {
347
351
 
348
352
  return parsedPath;
349
353
  }
354
+ function createURL(location) {
355
+ // window.location.origin is "null" (the literal string value) in Firefox
356
+ // under certain conditions, notably when serving from a local HTML file
357
+ // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
358
+ let base = typeof window !== "undefined" && typeof window.location !== "undefined" && window.location.origin !== "null" ? window.location.origin : "unknown://unknown";
359
+ let href = typeof location === "string" ? location : createPath(location);
360
+ return new URL(href, base);
361
+ }
350
362
 
351
363
  function getUrlBasedHistory(getLocation, createHref, validateLocation, options) {
352
364
  if (options === void 0) {
@@ -437,6 +449,16 @@ function getUrlBasedHistory(getLocation, createHref, validateLocation, options)
437
449
  return createHref(window, to);
438
450
  },
439
451
 
452
+ encodeLocation(location) {
453
+ // Encode a Location the same way window.location would
454
+ let url = createURL(createPath(location));
455
+ return _extends({}, location, {
456
+ pathname: url.pathname,
457
+ search: url.search,
458
+ hash: url.hash
459
+ });
460
+ },
461
+
440
462
  push,
441
463
  replace,
442
464
 
@@ -518,9 +540,12 @@ function matchRoutes(routes, locationArg, basename) {
518
540
  let matches = null;
519
541
 
520
542
  for (let i = 0; matches == null && i < branches.length; ++i) {
521
- matches = matchRouteBranch(branches[i], // incoming pathnames are always encoded from either window.location or
522
- // from route.navigate, but we want to match against the unencoded paths
523
- // in the route definitions
543
+ matches = matchRouteBranch(branches[i], // Incoming pathnames are generally encoded from either window.location
544
+ // or from router.navigate, but we want to match against the unencoded
545
+ // paths in the route definitions. Memory router locations won't be
546
+ // encoded here but there also shouldn't be anything to decode so this
547
+ // should be a safe operation. This avoids needing matchRoutes to be
548
+ // history-aware.
524
549
  safelyDecodeURI(pathname));
525
550
  }
526
551
 
@@ -1438,12 +1463,7 @@ function createRouter(init) {
1438
1463
  // the same encoding we'd get from a history.pushState/window.location read
1439
1464
  // without having to touch history
1440
1465
 
1441
- let url = createURL(createPath(location));
1442
- location = _extends({}, location, {
1443
- pathname: url.pathname,
1444
- search: url.search,
1445
- hash: url.hash
1446
- });
1466
+ location = init.history.encodeLocation(location);
1447
1467
  let historyAction = (opts && opts.replace) === true || submission != null ? Action.Replace : Action.Push;
1448
1468
  let preventScrollReset = opts && "preventScrollReset" in opts ? opts.preventScrollReset === true : undefined;
1449
1469
  return await startNavigation(historyAction, location, {
@@ -2516,8 +2536,6 @@ function unstable_createStaticHandler(routes) {
2516
2536
  let result;
2517
2537
 
2518
2538
  if (!actionMatch.route.action) {
2519
- let href = createServerHref(new URL(request.url));
2520
-
2521
2539
  if (isRouteRequest) {
2522
2540
  throw createRouterErrorResponse(null, {
2523
2541
  status: 405,
@@ -2525,7 +2543,7 @@ function unstable_createStaticHandler(routes) {
2525
2543
  });
2526
2544
  }
2527
2545
 
2528
- result = getMethodNotAllowedResult(href);
2546
+ result = getMethodNotAllowedResult(request.url);
2529
2547
  } else {
2530
2548
  result = await callLoaderOrAction("action", request, actionMatch, matches, undefined, // Basename not currently supported in static handlers
2531
2549
  true, isRouteRequest);
@@ -2709,7 +2727,7 @@ function normalizeNavigateOptions(to, opts, isFetcher) {
2709
2727
  path,
2710
2728
  submission: {
2711
2729
  formMethod: opts.formMethod,
2712
- formAction: createServerHref(parsePath(path)),
2730
+ formAction: stripHashFromPath(path),
2713
2731
  formEncType: opts && opts.formEncType || "application/x-www-form-urlencoded",
2714
2732
  formData: opts.formData
2715
2733
  }
@@ -2989,7 +3007,7 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
2989
3007
  }
2990
3008
 
2991
3009
  function createRequest(location, signal, submission) {
2992
- let url = createURL(location).toString();
3010
+ let url = createURL(stripHashFromPath(location)).toString();
2993
3011
  let init = {
2994
3012
  signal
2995
3013
  };
@@ -3182,7 +3200,7 @@ function getMethodNotAllowedMatches(routes) {
3182
3200
  }
3183
3201
 
3184
3202
  function getMethodNotAllowedResult(path) {
3185
- let href = typeof path === "string" ? path : createServerHref(path);
3203
+ let href = typeof path === "string" ? path : createPath(path);
3186
3204
  console.warn("You're trying to submit to a route that does not have an action. To " + "fix this, please add an `action` function to the route for " + ("[" + href + "]"));
3187
3205
  return {
3188
3206
  type: ResultType.error,
@@ -3199,11 +3217,13 @@ function findRedirect(results) {
3199
3217
  return result;
3200
3218
  }
3201
3219
  }
3202
- } // Create an href to represent a "server" URL without the hash
3203
-
3220
+ }
3204
3221
 
3205
- function createServerHref(location) {
3206
- return (location.pathname || "") + (location.search || "");
3222
+ function stripHashFromPath(path) {
3223
+ let parsedPath = typeof path === "string" ? parsePath(path) : path;
3224
+ return createPath(_extends({}, parsedPath, {
3225
+ hash: ""
3226
+ }));
3207
3227
  }
3208
3228
 
3209
3229
  function isHashChangeOnly(a, b) {
@@ -3321,12 +3341,6 @@ function getTargetMatch(matches, location) {
3321
3341
 
3322
3342
  let pathMatches = getPathContributingMatches(matches);
3323
3343
  return pathMatches[pathMatches.length - 1];
3324
- }
3325
-
3326
- function createURL(location) {
3327
- let base = typeof window !== "undefined" && typeof window.location !== "undefined" ? window.location.origin : "unknown://unknown";
3328
- let href = typeof location === "string" ? location : createServerHref(location);
3329
- return new URL(href, base);
3330
3344
  } //#endregion
3331
3345
 
3332
3346
  export { AbortedDeferredError, Action, ErrorResponse, IDLE_FETCHER, IDLE_NAVIGATION, convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes, getPathContributingMatches as UNSAFE_getPathContributingMatches, createBrowserHistory, createHashHistory, createMemoryHistory, createPath, createRouter, defer, generatePath, getStaticContextFromError, getToPathname, invariant, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, parsePath, redirect, resolvePath, resolveTo, stripBasename, unstable_createStaticHandler, warning };