Skip to content

Commit

Permalink
feat: add hash argument to routes command
Browse files Browse the repository at this point in the history
  • Loading branch information
jaulz committed Apr 18, 2023
1 parent 164ba23 commit 3815457
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
11 changes: 9 additions & 2 deletions packages/laravel/src/Console/Commands/PrintRoutesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PrintRoutesCommand extends Command
*
* @var string
*/
public $signature = 'navigare:routes {--ssr : Resolve paths for SSR.} {--export= : Path to a file to write the routes into.}';
public $signature = 'navigare:routes {--ssr : Resolve paths for SSR.} {--export= : Path to a file to write the routes into.} {--hash= : Hash of stringified routes.}';

/**
* The console command description.
Expand All @@ -39,14 +39,21 @@ public function handle()
{
$routes = $this->getRoutesAsJSON();

// Stop early if hash is matching
if (md5($routes) === $this->option('hash', '')) {
return self::SUCCESS;
}

// Export if requested
if ($path = $this->option('export')) {
File::put($path, $routes);

$this->info("Route file written to <comment>${path}</comment>.");
$this->info("Route file written to <comment>{$path}</comment>.");

return self::SUCCESS;
}

// And always output the routes
$this->output->write($routes);

return self::SUCCESS;
Expand Down
2 changes: 1 addition & 1 deletion packages/laravel/src/View/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function resolveSSRPath(Configuration $configuration): string
*/
public function resolveClientPath(Configuration $configuration): string
{
$path = $this->path . '?timestamp=' . Carbon::now()->timestamp;
$path = $this->path; // . '?timestamp=' . Carbon::now()->timestamp;

if ($manifest = $configuration->getClientManifest()) {
$path = $manifest->resolve($this->path);
Expand Down
27 changes: 26 additions & 1 deletion packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from '@babel/types'
import { RawRoute, RawRoutes } from '@navigare/core'
import { Server as SSRServer, serveSSR } from '@navigare/ssr'
import crypto from 'crypto'
import makeDebugger from 'debug'
import getPort from 'get-port'
import globby from 'globby'
Expand Down Expand Up @@ -68,6 +69,7 @@ export default function cresateNavigarePlugin(options: Options = {}): Plugin {
let currentRoutes: RawRoutes = isObject(resolvedOptions.routes)
? resolvedOptions.routes
: {}
let currentRoutesHash = ''
let currentConfiguration: Configuration | null = isObject(
resolvedOptions.configuration,
)
Expand Down Expand Up @@ -117,9 +119,22 @@ export default function cresateNavigarePlugin(options: Options = {}): Plugin {
// Read routes regularly in case no routes were provided
const updateRoutes = async () => {
try {
currentRoutes = await getRoutes(resolvedOptions, env, ssr)
const nextRoutes = await getRoutes(
resolvedOptions,
env,
ssr,
currentRoutesHash,
)
debug('read routes: %O', currentRoutes)

if (nextRoutes) {
currentRoutes = nextRoutes
currentRoutesHash = crypto
.createHash('md5')
.update(JSON.stringify(nextRoutes))
.digest('hex')
}

// Write types
if (currentRoutes) {
// await writeTypes(currentConfiguration?.types.path, currentRoutes)
Expand Down Expand Up @@ -379,6 +394,16 @@ export default function cresateNavigarePlugin(options: Options = {}): Plugin {
})
}

// Update path of used components based on the build manifest
if (environment.command === 'serve') {
rawRoute.components = rawRoute.components?.map((component) => {
return {
...component,
path: `${component.path}?timestamp=${Date.now()}`,
}
})
}

// Replace route name with the raw route
routeCall.arguments[0] = identifier(JSON.stringify(rawRoute))

Expand Down
15 changes: 12 additions & 3 deletions packages/vite/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export const getRoutes = async (
options: Options,
env: NodeJS.ProcessEnv,
ssr: boolean,
): Promise<RawRoutes> => {
hash?: string,
): Promise<RawRoutes | undefined> => {
// Prefer already defined routes
if (isObject(options.routes)) {
return options.routes
Expand All @@ -78,7 +79,7 @@ export const getRoutes = async (
// Otherwise try to retrieve them from adapter
switch (options.routes) {
case Adapter.Laravel: {
return await getLaravelRoutes(options, env, ssr)
return await getLaravelRoutes(options, env, ssr, hash)
break
}

Expand All @@ -95,14 +96,22 @@ export async function getLaravelRoutes(
options: Options,
env: NodeJS.ProcessEnv,
ssr: boolean,
hash?: string,
): Promise<RawRoutes> {
const executable = findPhpPath({ env, path: options.php })

try {
// Asks artisan for the routes
debug('reading Laravel routes')
const json = JSON.parse(
callArtisan(executable, 'navigare:routes', ssr ? '--ssr' : undefined),
callArtisan(
executable,
...[
'navigare:routes',
ssr ? '--ssr' : undefined,
hash ? `--hash ${hash}` : undefined,
],
),
) as RawRoutes

if (!json) {
Expand Down

0 comments on commit 3815457

Please sign in to comment.